Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dsp_fir.py 2.7KB

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