Skip to content

Instantly share code, notes, and snippets.

@NT7S
Last active December 1, 2019 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NT7S/bc37333dda3fdb4a2ef43a1e54e9b50c to your computer and use it in GitHub Desktop.
Save NT7S/bc37333dda3fdb4a2ef43a1e54e9b50c to your computer and use it in GitHub Desktop.
FIR Filter Example
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import numpy as np
import simpleaudio as sa
import time
# Set up audio params
sample_rate = 44100
sound_length = 5 #seconds
t = np.linspace(0, sound_length, sound_length * sample_rate, False)
# Define signals
wave1_freq = 4000
wave1_amplitude = 2
wave1_phi = 0
wave1 = wave1_amplitude * np.sin(wave1_freq * t * 2 * np.pi + wave1_phi)
wave2_freq = 1600
wave2_amplitude = 0.8
wave2_phi = 0
wave2 = wave2_amplitude * np.sin(wave2_freq * t * 2 * np.pi + wave2_phi)
wave3_freq = 600
wave3_amplitude = 1.2
wave3_phi = 0.4
wave3 = wave3_amplitude * np.sin(wave3_freq * t * 2 * np.pi + wave3_phi)
# Combine signals
signal = np.add(wave1, wave2)
signal = np.add(signal, wave3)
taps = np.array([
0.0003745900908163193, \
0.001035951933453526, \
0.0007325310976027528, \
0.0012734899113014343, \
0.001505827537138896, \
0.001906701284701609, \
0.002255193914554153, \
0.002622053674575616, \
0.0029498890263962266, \
0.0032316494614751663, \
0.0034367455949670237, \
0.003543326323203393, \
0.0035271808853288783, \
0.003366493641359242, \
0.0030440547958260227, \
0.002546845932748022, \
0.0018699255183142716, \
0.0010145993691660009, \
-0.000007822740186522711, \
-0.0011772398017138564, \
-0.0024623894663077057, \
-0.0038224245149365193, \
-0.005207993967976202, \
-0.006559950494834579, \
-0.007813025767998694, \
-0.00889630342344623, \
-0.009737673508070597, \
-0.010263527022136008, \
-0.010403904283509603, \
-0.010094690943920976, \
-0.009281193138939584, \
-0.007920352308374777, \
-0.005982648536411453, \
-0.0034573396113584203, \
-0.0003501983730495297, \
0.0033130673334052624, \
0.007486572786566192, \
0.012105787794150783, \
0.017087762369887797, \
0.022333435670723732, \
0.027729793775114597, \
0.0331544017002729, \
0.03847753819629622, \
0.04356671732492255, \
0.04829283271615901, \
0.0525316528144209, \
0.05616977310170035, \
0.059109496837266275, \
0.061269060239080915, \
0.06258934720612876, \
0.06303403098445609, \
0.06258934720612876, \
0.061269060239080915, \
0.059109496837266275, \
0.05616977310170035, \
0.0525316528144209, \
0.04829283271615901, \
0.04356671732492255, \
0.03847753819629622, \
0.0331544017002729, \
0.027729793775114597, \
0.022333435670723732, \
0.017087762369887797, \
0.012105787794150783, \
0.007486572786566192, \
0.0033130673334052624, \
-0.0003501983730495297, \
-0.0034573396113584203, \
-0.005982648536411453, \
-0.007920352308374777, \
-0.009281193138939584, \
-0.010094690943920976, \
-0.010403904283509603, \
-0.010263527022136008, \
-0.009737673508070597, \
-0.00889630342344623, \
-0.007813025767998694, \
-0.006559950494834579, \
-0.005207993967976202, \
-0.0038224245149365193, \
-0.0024623894663077057, \
-0.0011772398017138564, \
-0.000007822740186522711, \
0.0010145993691660009, \
0.0018699255183142716, \
0.002546845932748022, \
0.0030440547958260227, \
0.003366493641359242, \
0.0035271808853288783, \
0.003543326323203393, \
0.0034367455949670237, \
0.0032316494614751663, \
0.0029498890263962266, \
0.002622053674575616, \
0.002255193914554153, \
0.001906701284701609, \
0.001505827537138896, \
0.0012734899113014343, \
0.0007325310976027528, \
0.001035951933453526, \
0.0003745900908163193])
# Execute the filter
output = np.zeros(signal.size - taps.size)
for i in range(output.size):
output[i] = np.inner(taps, signal[taps.size + i:i:-1])
# Normalize audio level to 16 bit int for simpleaudio
signal *= 32767 / np.max(np.abs(signal))
signal = signal.astype(np.int16)
# Start playback then wait for finish
play_obj = sa.play_buffer(signal, 1, 2, sample_rate)
play_obj.wait_done()
# # Now wait one second
time.sleep(1)
# Normalize audio level to 16 bit int for simpleaudio
output *= 32767 / np.max(np.abs(output))
output = output.astype(np.int16)
# print(signal[:300])
# print(output[:300])
# Start playback then wait for finish
play_obj = sa.play_buffer(output, 1, 2, sample_rate)
play_obj.wait_done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment