Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:1bec1c13107e, committed 2019-10-23
- Comitter:
- gemmaro
- Date:
- Wed Oct 23 02:01:18 2019 +0000
- Parent:
- 0:f0a3e5729b8f
- Commit message:
- test not yet
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Oct 16 04:09:09 2019 +0000 +++ b/main.cpp Wed Oct 23 02:01:18 2019 +0000 @@ -23,118 +23,167 @@ double b[50]; double w[50]; -void wait100us(void) { - while (t.read_us() < dt_us) {}; +namespace lib +{ + +void wait100us(void) +{ + while (t.read_us() < dt_us) { + }; t.reset(); } -void rec(void) { +void rec(void) +{ unsigned short InDat; int i; - + /// recording myled1 = 1; for (i = 0; i < DATA_SIZE; ++i) { InDat = MicIn.read_u16() >> 4; data[i] = InDat; - wait100us(); + lib::wait100us(); } myled1 = 0; } -void get_offset(void) { +void get_offset(void) +{ unsigned short InDat; double a = 0; int i; for (i = 0; i < 1000; ++i) { InDat = MicIn.read_u16() >> 4; a += (double)InDat; - wait100us(); + lib::wait100us(); } offset = (unsigned int)(a / 1000); printf("%d\n\r", offset); } -void hanning_window(int window_size) { +void hanning_window(int window_size) +{ int i; for (i = 0; i < window_size; ++i) { w[i] = 0.5 - 0.5 * cos(2 * PI * ((i + 1) - 0.5) / window_size); } } -int lps(int k, int offsets, int fc) { +namespace filter +{ + +int low_pass(int k, int offsets, int fc) +{ if (k == offsets) { return 2 * fc; } - return 2 * fc * sin(2 * PI * fc * (k - offsets)) / (2 * PI * fc * (k - offsets)); + return 2 * fc * sin(2 * PI * fc * (k - offsets)) / + (2 * PI * fc * (k - offsets)); } -int high_pass_filter(int k, int offsets, int fc) { +int high_pass(int k, int offsets, int fc) +{ if (k == offsets) { return 1 - 2 * fc; } return -2 * fc * sin(2 * PI * fc * k) / (2 * PI * fc * k); } -int band_pass_filter(int k, int offsets, int fc1, int fc2) { +int band_pass(int k, int offsets, int fc1, int fc2) +{ if (k == offsets) { return 2 * (fc2 - fc1); } - return 2 * fc2 * sin(2 * PI * fc2 * k) / (2 * PI * fc2 * k) - 2 * fc1 * sin(2 * PI * fc1 * k) / (2 * PI * fc1 * k); + return 2 * fc2 * sin(2 * PI * fc2 * k) / (2 * PI * fc2 * k) - + 2 * fc1 * sin(2 * PI * fc1 * k) / (2 * PI * fc1 * k); } -int band_eliminate_filter(int k, int offsets, int fc1, int fc2) { +int band_eliminate(int k, int offsets, int fc1, int fc2) +{ if (k == offsets) { return 1 - 2 * (fc2 - fc1); } - return 2 * fc1 * sin(2 * PI * fc1 * k) / (2 * PI * fc1 * k) - 2 * fc2 * sin(2 * PI * fc2 * k) / (2 * PI * fc2 * k); + return 2 * fc1 * sin(2 * PI * fc1 * k) / (2 * PI * fc1 * k) - + 2 * fc2 * sin(2 * PI * fc2 * k) / (2 * PI * fc2 * k); } -void fir_lpf(int fs, int fe, int delta) { +} // namespace filter + +void finite_impulse_response(int fs, int fe, int delta) +{ int offsets; int k; float fc, delta_s; - - fc = (((float)fe +(float)delta / 2) / fs); + + fc = (((float)fe + (float)delta / 2) / fs); delta_s = ((float)delta / fs); filter_size = (int)(3.1 / delta_s); if (filter_size % 2 == 0) filter_size += 1; - hanning_window(filter_size); - + lib::hanning_window(filter_size); + offsets = (filter_size - 1) / 2; for (k = 0; k < filter_size; ++k) { - b[k] = lps(k, offsets, fc); + b[k] = lib::filter::low_pass(k, offsets, fc); } - + for (k = 0; k < filter_size; ++k) { b[k] *= w[k]; } } -int main() { +void finite_impulse_response2(int fs, int fe1, int fe2, int delta) +{ + int offsets; + int k; + float fc1, fc2, delta_s; + + fc1 = (((float)fe1 + (float)delta / 2) / fs); + fc2 = (((float)fe2 + (float)delta / 2) / fs); + delta_s = ((float)delta / fs); + filter_size = (int)(3.1 / delta_s); + if (filter_size % 2 == 0) filter_size += 1; + lib::hanning_window(filter_size); + + offsets = (filter_size - 1) / 2; + for (k = 0; k < filter_size; ++k) { + b[k] = lib::filter::band_pass(k, offsets, fc1, fc2); + } + + for (k = 0; k < filter_size; ++k) { + b[k] *= w[k]; + } +} + +} // namespace lib + +int main() +{ unsigned short InDat, OutDat; - int fe = 100; + int fe1 = 500; + int fe2 = 600; int delta = 1000; int fs = 8000; int n, k; double y; - + /// Timer interrupt t.start(); t.reset(); - get_offset(); - + lib::get_offset(); + /// Filter parameter myled1 = 1; - fir_lpf(fs, fe, delta); + // lib::finite_impulse_response(fs, fe1, delta); + lib::finite_impulse_response2(fs, fe1, fe2, delta); myled1 = 0; - while(1) { + while (1) { myled2 = 1; - rec(); + lib::rec(); wait(2); myled2 = 0; - + /// filter myled3 = 1; for (n = filter_size; n < DATA_SIZE; ++n) { @@ -145,8 +194,8 @@ InDat = y + offset; OutDat = InDat >> 2; HeadPhoneOut.write_u16(OutDat << 6); - wait100us(); + lib::wait100us(); } myled3 = 0; } -} +} \ No newline at end of file