Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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