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