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