5

I'm doing a project about speech processing. I wonder how to create a white noise signal in Python, and filter the noise signal with bandpass filter?

import pyaudio
import numpy as np
import scipy.signal as signal

CHUNK = 64 #the block size
Q = 50

pa = pyaudio.PyAudio()
stream = pa.open(format=pyaudio.paFloat32,
                            channels=1,
                            rate=44100,
                            output=True)
while True:
    noise = np.random.uniform(-1,1,CHUNK)
    b,a = signal.iirfilter(1,[2*500*(1-1/(2*Q))/44100,2*500*(1+1/(2*Q))/44100])
    output = signal.lfilter(b,a,noise)
    output.astype(np.float32)
    output = output.tobytes()
    stream.write(output)

Is the code right? And what is the difference between signal.iirfilter and signal.butter? Thank you very much.

Roger
  • 91
  • 2
  • 4
  • Is this a signal processing question or a coding question? You might want to search/ask over at https://dsp.stackexchange.com/. white noise == random amplitude/period..?? – wwii Sep 06 '17 at 22:57
  • 1
    It is a code question. I want to create Python code to generate a white noise signal and filter it. – Roger Sep 07 '17 at 12:45
  • 2
    How do you define white noise and what type of filter do you want to apply? Does the solution you provided give the desired result? If not what is wrong with it? What is the expected result? Can you provide a minimal example of the input? Please read [mcve] and [ask]. – wwii Sep 07 '17 at 13:29
  • 1
    Great Python question for StackOverflow. The white noise def is on Wikipedia. Here is an example of white noise wav file: https://drive.google.com/file/d/1k7LKEYzzIg6l6nbil4b5oVMyy9gFlWcF/view?usp=sharing – Oleg Melnikov Jan 05 '18 at 17:15

0 Answers0