I'm dealing with the problem of filtering a pink noise with a peak filter (the final goal is to equalize an input signal). I've been designing the peak filter myself to get more familiar with 1D signal filtering. As for the pink noise, I'm using pink noise generated through the work already done in https://github.com/felixpatzelt/colorednoise/blob/master/colorednoise.py .
1/ I am wondering why the intensity of the power spectrogram is random when I run several times the same function with the same paramaters, is it due to the call to numpy.random.normal
? How would someone proceed to generate a noise in a specified range of values ?
The two following pictures show that the power range is inconstant through different runs (cannot post images directly, sorry for the imgur links) :
2/ Following is the definition of the peak filter I use further :
class PeakFilter():
def __init__(self, center_freq, sample_rate, Q, gainDB):
self.center_freq = center_freq
self.sample_rate = sample_rate
self.gainDB = gainDB
self.Q = Q
# derived quantities
self.A = 10**(self.gainDB/40)
self.center_freq = 2*np.pi * self.center_freq / self.sample_rate
def peak_transfer(self):
b = np.array([ 1, self.A/self.Q, 1 ])
a = np.array([ 1, 1/(self.A*self.Q), 1])
# shift to center frequency
b, a = signal.lp2lp(b, a, wo=self.center_freq)
return b, a
sample_rate = 44100
fc = 1000
Q = 1.7
Gdb = 5
peak_filt = PeakFilter(fc, sample_rate, Q, Gdb)
b, a = peak_filt.peak_transfer()
w, h = signal.freqs(b, a, 10000)
freq = w*sample_rate/2/np.pi
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(freq, 20*np.log10(abs(h)), color='blue')
ax.set_xscale('log')
plt.grid('true')
plt.plot()
The filter response has the expected shape :
https://i.stack.imgur.com/OUsen.jpg
However, when I am filtering the noise by this filter with this code:
out = signal.filtfilt(b, a, noise)
plt.plot(out, color='red')
I get the following result:
https://i.stack.imgur.com/Q8GuN.jpg
This is were I find difficult to judge if this is correct. First I do not know why the filtered noise gets this shape and such values (negative as well). Also, I would expect the filtered signal to vary in shape or intensity, with Gdb
and Q
, at the given center frequency, but it does not.
There should be attenuation or boost just around this frequency (if I got it right).