BA
/
BaBoRo_test2
Backup 1
mbed-os/tools/dev/dsp_fir.py@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | """ |
borlanic | 0:02dd72d1d465 | 2 | mbed SDK |
borlanic | 0:02dd72d1d465 | 3 | Copyright (c) 2011-2013 ARM Limited |
borlanic | 0:02dd72d1d465 | 4 | |
borlanic | 0:02dd72d1d465 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:02dd72d1d465 | 6 | you may not use this file except in compliance with the License. |
borlanic | 0:02dd72d1d465 | 7 | You may obtain a copy of the License at |
borlanic | 0:02dd72d1d465 | 8 | |
borlanic | 0:02dd72d1d465 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:02dd72d1d465 | 10 | |
borlanic | 0:02dd72d1d465 | 11 | Unless required by applicable law or agreed to in writing, software |
borlanic | 0:02dd72d1d465 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:02dd72d1d465 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:02dd72d1d465 | 14 | See the License for the specific language governing permissions and |
borlanic | 0:02dd72d1d465 | 15 | limitations under the License. |
borlanic | 0:02dd72d1d465 | 16 | """ |
borlanic | 0:02dd72d1d465 | 17 | from numpy import sin, arange, pi |
borlanic | 0:02dd72d1d465 | 18 | from scipy.signal import lfilter, firwin |
borlanic | 0:02dd72d1d465 | 19 | from pylab import figure, plot, grid, show |
borlanic | 0:02dd72d1d465 | 20 | |
borlanic | 0:02dd72d1d465 | 21 | #------------------------------------------------ |
borlanic | 0:02dd72d1d465 | 22 | # Create a signal for demonstration. |
borlanic | 0:02dd72d1d465 | 23 | #------------------------------------------------ |
borlanic | 0:02dd72d1d465 | 24 | # 320 samples of (1000Hz + 15000 Hz) at 48 kHz |
borlanic | 0:02dd72d1d465 | 25 | sample_rate = 48000. |
borlanic | 0:02dd72d1d465 | 26 | nsamples = 320 |
borlanic | 0:02dd72d1d465 | 27 | |
borlanic | 0:02dd72d1d465 | 28 | F_1KHz = 1000. |
borlanic | 0:02dd72d1d465 | 29 | A_1KHz = 1.0 |
borlanic | 0:02dd72d1d465 | 30 | |
borlanic | 0:02dd72d1d465 | 31 | F_15KHz = 15000. |
borlanic | 0:02dd72d1d465 | 32 | A_15KHz = 0.5 |
borlanic | 0:02dd72d1d465 | 33 | |
borlanic | 0:02dd72d1d465 | 34 | t = arange(nsamples) / sample_rate |
borlanic | 0:02dd72d1d465 | 35 | signal = A_1KHz * sin(2*pi*F_1KHz*t) + A_15KHz*sin(2*pi*F_15KHz*t) |
borlanic | 0:02dd72d1d465 | 36 | |
borlanic | 0:02dd72d1d465 | 37 | #------------------------------------------------ |
borlanic | 0:02dd72d1d465 | 38 | # Create a FIR filter and apply it to signal. |
borlanic | 0:02dd72d1d465 | 39 | #------------------------------------------------ |
borlanic | 0:02dd72d1d465 | 40 | # The Nyquist rate of the signal. |
borlanic | 0:02dd72d1d465 | 41 | nyq_rate = sample_rate / 2. |
borlanic | 0:02dd72d1d465 | 42 | |
borlanic | 0:02dd72d1d465 | 43 | # The cutoff frequency of the filter: 6KHz |
borlanic | 0:02dd72d1d465 | 44 | cutoff_hz = 6000.0 |
borlanic | 0:02dd72d1d465 | 45 | |
borlanic | 0:02dd72d1d465 | 46 | # Length of the filter (number of coefficients, i.e. the filter order + 1) |
borlanic | 0:02dd72d1d465 | 47 | numtaps = 29 |
borlanic | 0:02dd72d1d465 | 48 | |
borlanic | 0:02dd72d1d465 | 49 | # Use firwin to create a lowpass FIR filter |
borlanic | 0:02dd72d1d465 | 50 | fir_coeff = firwin(numtaps, cutoff_hz/nyq_rate) |
borlanic | 0:02dd72d1d465 | 51 | |
borlanic | 0:02dd72d1d465 | 52 | # Use lfilter to filter the signal with the FIR filter |
borlanic | 0:02dd72d1d465 | 53 | filtered_signal = lfilter(fir_coeff, 1.0, signal) |
borlanic | 0:02dd72d1d465 | 54 | |
borlanic | 0:02dd72d1d465 | 55 | #------------------------------------------------ |
borlanic | 0:02dd72d1d465 | 56 | # Plot the original and filtered signals. |
borlanic | 0:02dd72d1d465 | 57 | #------------------------------------------------ |
borlanic | 0:02dd72d1d465 | 58 | |
borlanic | 0:02dd72d1d465 | 59 | # The first N-1 samples are "corrupted" by the initial conditions |
borlanic | 0:02dd72d1d465 | 60 | warmup = numtaps - 1 |
borlanic | 0:02dd72d1d465 | 61 | |
borlanic | 0:02dd72d1d465 | 62 | # The phase delay of the filtered signal |
borlanic | 0:02dd72d1d465 | 63 | delay = (warmup / 2) / sample_rate |
borlanic | 0:02dd72d1d465 | 64 | |
borlanic | 0:02dd72d1d465 | 65 | figure(1) |
borlanic | 0:02dd72d1d465 | 66 | # Plot the original signal |
borlanic | 0:02dd72d1d465 | 67 | plot(t, signal) |
borlanic | 0:02dd72d1d465 | 68 | |
borlanic | 0:02dd72d1d465 | 69 | # Plot the filtered signal, shifted to compensate for the phase delay |
borlanic | 0:02dd72d1d465 | 70 | plot(t-delay, filtered_signal, 'r-') |
borlanic | 0:02dd72d1d465 | 71 | |
borlanic | 0:02dd72d1d465 | 72 | # Plot just the "good" part of the filtered signal. The first N-1 |
borlanic | 0:02dd72d1d465 | 73 | # samples are "corrupted" by the initial conditions. |
borlanic | 0:02dd72d1d465 | 74 | plot(t[warmup:]-delay, filtered_signal[warmup:], 'g', linewidth=4) |
borlanic | 0:02dd72d1d465 | 75 | |
borlanic | 0:02dd72d1d465 | 76 | grid(True) |
borlanic | 0:02dd72d1d465 | 77 | |
borlanic | 0:02dd72d1d465 | 78 | show() |
borlanic | 0:02dd72d1d465 | 79 | |
borlanic | 0:02dd72d1d465 | 80 | #------------------------------------------------ |
borlanic | 0:02dd72d1d465 | 81 | # Print values |
borlanic | 0:02dd72d1d465 | 82 | #------------------------------------------------ |
borlanic | 0:02dd72d1d465 | 83 | def print_values(label, values): |
borlanic | 0:02dd72d1d465 | 84 | var = "float32_t %s[%d]" % (label, len(values)) |
borlanic | 0:02dd72d1d465 | 85 | print "%-30s = {%s}" % (var, ', '.join(["%+.10f" % x for x in values])) |
borlanic | 0:02dd72d1d465 | 86 | |
borlanic | 0:02dd72d1d465 | 87 | print_values('signal', signal) |
borlanic | 0:02dd72d1d465 | 88 | print_values('fir_coeff', fir_coeff) |
borlanic | 0:02dd72d1d465 | 89 | print_values('filtered_signal', filtered_signal) |