dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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