can anyone explain the difference between fft and freqz commands?
Show older comments
can anyone explain the difference between fft and freqz commands?
Accepted Answer
More Answers (1)
freqz is a general purpose function for computing the frequency response of a discrete-time filter. The filter can have an infinite duration impulse response (IIR) or a finite duration impulse response (FIR).
For a FIR filter (with typical scaling and implementation), the filter coefficients are the elements of the impulse response, or, alternatively, the impulse response defines the filter coefficients.
For example, let h[n] be the finite duration impulse response of a FIR filter with h[n] defined as
hn = @(n) (5-n).*(n>=0 & n<=10);
The values of h[n] that we care about are then
n = 0:10;
h = hn(n);
The numerator and denominator of the FIR filter with this impulse response are simply
b = h;
a = 1;
Verify that this filter has the impulse response h[n]
figure
impz(b,a,20)
hold on
stem(0:19,hn(0:19))
The frequency response of the filter H(z) = b(z)/a(z) can be computed via freqz (for either IIR or FIR). The frequency response can also be computed as the Discrete Time Fourier Transform (DTFT) of the filter impulse response, h[n].
The independent variable of the DTFT of h[n], or equivalently the frequency response of H(z), is continuous and covers the entire real line. Furthermore, the DTFT of h[n] (or frequency response of H(z)) is periodic with period 2*pi rad/sample. If h[n] is real valued, the DTFT (or frequency response) will have symmetry over one period, so in this case it's common to only plot the response over a half-period, typically from 0 to pi.
We can use freqz to compute the DTFT of b(z)/a(z) (or frequency response) over whatever frequency range we want at whatever frequencies we desire. If we want equally spaced frequencies, say 1024 of them, over the full period frequency interval 0-2*pi, then we have
[H,w] = freqz(h,1,1024,'whole'); % same as freqz(b,a,1024,'whole');
If we only want the response from 0 - pi rad/sample, then omit the 'whole' option.
Plot the gain and phase of the frequency response
figure
hax1 = subplot(211);
plot(hax1,w,abs(H));
hold(hax1,'on')
hax2 = subplot(212);
plot(hax2,w,angle(H));
hold(hax2,'on');
xlabel('omega (rad/sample)')
For a finite duration signal, h[n], with h[n] = 0 for n < 0 (i.e., the response of a causal system), the function fft computes the Discrete Fourier Transform (DFT) of h[n] for values of h[n] from n = 0 to N, with N greater than or equal to the length of the signal, which is 11 in this example.
hDFT = fft(h);
The DFT itself is a function of an integer index. Each element of the DFT can be viewed as corresponding to the elements of the discrete frequency vector given by
N = numel(hDFT);
wdft = (0:N-1)/N*2*pi; % (rad/sample)
Each element of hDFT is actually a frequency domain sample of H, the DTFT of h[n]
stem(hax1,wdft,abs(hDFT))
stem(hax2,wdft,angle(hDFT))
If we zero pad the DFT, let's say to 41 samples
hDFT2 = fft(h,41);
Then we have the frequency vector
N = numel(hDFT2);
wdft2 = (0:N-1)/N*2*pi;
stem(hax1,wdft2,abs(hDFT2))
legend(hax1,'DTFT','DFT','DFT2','Location','North')
stem(hax2,wdft2,angle(hDFT2))
and then we just have more equally spaced, frequency domain samples of the DTFT of h[n].
It stands to reason that more and more zero padding causes the DFT samples of the DTFT to get closer and closer together, and in the limit is the DTFT (for a finite duration signal). In fact, in some cases freqz just uses fft to compute its result (but not always depending on user inputs).
Summary: freqz computes the frequency response of a system represented by a rational transfer function, which is the DTFT of the system impulse response, at user-defined frequencies. If the impulse response is finite duration (and causal) then fft computes the DFT, which are equally spaced, frequency-domain samples of the DTFT. Or put another way, the fft of h[n] returns equally spaced frequency domain samples of the the DTFT of h[n] or the frequency response of the system that has h[n] as its impulse response.
Categories
Find more on Digital Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
