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