can anyone explain the difference between fft and freqz commands?
18 views (last 30 days)
Show older comments
can anyone explain the difference between fft and freqz commands?
0 Comments
Accepted Answer
Jan
on 19 Jan 2017
It is hard to resist to answer "yes".
Let's assume that you've read the docs of both command already. While freqz replies the "Frequency response of digital filter", fft determines the "Fast Fourier transform". The first command takes filter coefficients as input and the number of points for the response, while the later gets a signal as input.
Does this help already? If not, please edit the question (not as comment) and add any details about why you compare these 2 different functions. Perhaps it helps, when you explain what you see as similarities.
2 Comments
ed
on 13 Mar 2025
Edited: ed
on 13 Mar 2025
May I help to the question? what happens is that both a signal and the system (coeficients) are described in the same way, a vector of samples. If you see the vector, you may not distinguish if this vector contains coeficients or samples of a signal. Remember that the frecuency response is actually the fft of the system's impulse response. Therefore, theoretically fft and freqz may work for both.. and actually thery do, but I noticed that the freqz gives only half of the fft response and with a better resolution even when you put the N value the same in both functions: freqz(h,[1],N) or fft(h,N)
More Answers (1)
Paul
on 13 Mar 2025
Edited: Paul
on 14 Mar 2025
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.
0 Comments
See Also
Categories
Find more on Fourier Analysis and Filtering 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!
