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