Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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