diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2022-01-09 12:28:19 -0500 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2022-01-09 12:28:19 -0500 |
commit | 1b176cf6cd75c8031a140961655cdd3c16589a68 (patch) | |
tree | 8415664e40a9a768d8c3a35fd81252bfdefb72f9 /examples/2_convolve_overlap_save.cpp | |
parent | fde531e7c44ea917f745a9f800178fbe83fa19b5 (diff) |
small changes; sig gen square(), triangle(), pulse()
Diffstat (limited to 'examples/2_convolve_overlap_save.cpp')
-rw-r--r-- | examples/2_convolve_overlap_save.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/2_convolve_overlap_save.cpp b/examples/2_convolve_overlap_save.cpp new file mode 100644 index 0000000..57c020a --- /dev/null +++ b/examples/2_convolve_overlap_save.cpp @@ -0,0 +1,47 @@ +/** + * 2_convolve_overlap_save.cpp + * Written by Clyne Sullivan. + * + * This convolution examples takes an overlap-save approach, where samples from the previous run + * are saved so that the overall operation is not interrupted (i.e. the observed output will + * transition smoothly between processed "chunks"). + * + * Note that there are still improvements that can be made to the code; for example, notice every + * spot where an integer/float conversion is necessary. Operations like these may slow down the + * computation. + */ + +Sample *process_data(Samples samples) +{ + static Sample buffer[samples.size()]; + + constexpr unsigned int filter_size = 3; + float filter[filter_size] = { + 0.3333, 0.3333, 0.3333 + }; + + // Keep a buffer of extra samples for overlap-save + static Sample prev[filter_size]; + + for (int n = 0; n < samples.size(); n++) { + buffer[n] = 0; + + for (int k = 0; k < filter_size; k++) { + int i = n - (filter_size - 1) + k; + + // If i is >= 0, access current sample buffer. + // If i is < 0, provide the previous samples from the 'prev' buffer + if (i >= 0) + buffer[n] += samples[i] * filter[k]; + else + buffer[n] += prev[filter_size - 1 + i] * filter[k]; + } + } + + // Save samples for the next convolution run + for (int i = 0; i < filter_size; i++) + prev[i] = samples[samples.size() - filter_size + i]; + + return buffer; +} + |