| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | // Remove the upper-half of the spectrum (low-pass filter) |
| 8 | // and then halve the sample-rate. |
| 9 | // |
| 10 | // The input must contain twice the number of output samples plus some extra. |
| 11 | // in.size() == 2 * out.size() + HALF_BAND_EXTRA |
| 12 | // |
| 13 | // About overlapping input and output (calculate in-place): |
| 14 | // The output buffer is smaller than the input. Overlap is allowed, but only |
| 15 | // when the beginning of the output is aligned with the beginning of the input. |
| 16 | static constexpr size_t HALF_BAND_EXTRA = 13; // depends on the number of coefficients in the filter |
| 17 | inline void |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | // The filter coefficients were obtained via this octave (matlab) script: |
| 60 | /* ----- 8< ----- 8< ----- 8< ----- 8< ----- 8< ----- 8< ----- 8< ----- |
| 61 | |
| 62 | pkg load signal |
| 63 | |
| 64 | ntaps = 4 * 3 + 3; |
| 65 | beta = 1; |
| 66 | resolution = 1024; % relates to the 'resolution' of the plot |
| 67 | samplefreq = 100; |
| 68 | |
| 69 | |
| 70 | N= ntaps-1; |
| 71 | n= -N/2:N/2; |
| 72 | sinc= sin(n*pi/2)./(n*pi+eps); % truncated impulse response; eps= 2E-16 |
| 73 | sinc(N/2 +1)= 1/2; % value for n --> 0 |
| 74 | win= kaiser(ntaps, beta); % window function |
| 75 | b= sinc.*win'; |
| 76 | b(2:2:ntaps/2)=0; |
| 77 | b((ntaps+5)/2:2:ntaps)=0; |
| 78 | |
| 79 | b2=b; |
| 80 | b2((ntaps+1)/2)=0; |
| 81 | b2 = b2 / (2 * sum(b2)); |
| 82 | b2((ntaps+1)/2)=0.5; |
| 83 | b=b2; |
| 84 | |
| 85 | x = b; |
| 86 | x(N+1:resolution) = 0; |
| 87 | y = fft(x); |
| 88 | |
| 89 | xx = [0 : samplefreq/(resolution) : samplefreq/2]; |
| 90 | plot(xx, 20 * log(abs(y(1:resolution/2+1)))); |
| 91 | xlabel("frequency [kHz]"); |
| 92 | ylabel("attenuation [dB]"); |
| 93 | |
| 94 | % print result |
| 95 | format long |
| 96 | 2 * b |
| 97 | |
| 98 | ----- 8< ----- 8< ----- 8< ----- 8< ----- 8< ----- 8< ----- 8< ----- */ |