I try to design a FIR All-pass filter with random phase in the frequency domain. I am a bit confused by my result and am not sure if the reason is a programming error or a misconception about convolution and impulse response of myself. I use Python and Numpy:

import numpy as np

mag = np.ones(129) # magnitude is unitiy
phase = np.zeros(129)
random_numbers = (np.random.rand(128)-0.5)*2.0
phase[1:] = random_numbers * np.pi # phase is random except for dc offset
spec = mag*np.exp(phase * 1j) # get spectrum from magnitude and phase
coefficients = np.fft.irfft(spec) # do inverse fft to get coefficents, result is of length 256

If I do a fft on my coefficients the magnitude looks nearly right. My first question: Why is the highest frequency suppressed?

Magnitude Spectrum of coefficients

And secondly, if I use my coefficients for convolving with an impulse, I thought that should reproduce the coefficients? But it does not. Why are there these "random variations" around 1.0?

impulse = np.zeros(256, dtype=np.float64) # create impulse
impulse[256/2] = 1.0
result = np.convolve(impulse, coefficients, "same") # convolve it

Magnitude Spectrum of result of convolution of coefficients and impulse

Any explanations or hints are much appreciated!


1 Answer 1


The dip at Nyquist has to do with the fact that you didn't assign the value $0$ to the desired phase at Nyquist. You correctly assigned $0$ to the phase at DC, but you have to do the same at Nyquist, because a filter with real-valued coefficients has a real-valued frequency response at DC and Nyquist. So you just need 127 random numbers, not 128.

But the plot of the magnitude response of the designed filter is deceptive, because by taking an FFT (of length 256) of the coefficients you basically just plot the desired magnitude (apart from the Nyquist point), because you just invert your design process. Try to zero-pad your coefficients and take an FFT of length 1024 or more. You will be surprised to see that your filter doesn't at all resemble a very good all-pass filter. You see some indication of this in your second plot, which is more realistic.

Note that a non-trivial FIR filter (i.e. one which is not just a single delta impulse) can never have an exactly constant magnitude response, because its transfer function is a polynomial which can't be constant (unless it is a trivial polynomial). Your all-pass filter has an especially poor magnitude response because it is very hard for the filter to approximate those random phase jumps.

  • $\begingroup$ Thanks alot, my first problem could be easily fixed by your hint! If I understand right, the second problem, may lay deep in the approach in general... The result of your suggestion of zero-padding was really surprising – it looks even much worse than my second plot. I tried to implement it from that paper. The author mentiones that problem at pp. 75-76, but his plot (Figure 6a) looks less worse... Again, thanks! $\endgroup$ Commented Jul 1, 2014 at 8:26
  • $\begingroup$ @FrankZalkow: I'm not sure how important the random phase is for your application, but there should be some extra conditions such that the phase looks like what can be realistically achieved by an FIR filter. $\endgroup$
    – Matt L.
    Commented Jul 1, 2014 at 9:06
  • $\begingroup$ The random phase is quite important because one needs to generate multiple phases with a close-to-zero cross-correlation among each of them. $\endgroup$ Commented Jul 1, 2014 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.