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