- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
how to use freqz function correctly
82 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
Im trying to visualize the frequency response using the freqz function for a filter. My sampling frequency is 15 MHz, while the signal frequency is only 1 kHz. Due to this large difference between the sampling frequency and the signal frequency, the resulting frequency response plot is not clear. Is there anything i can do ?
Im using freqz(x), in the command window.
0 comentarios
Respuestas (3)
Hassaan
el 10 de En. de 2024
Editada: Hassaan
el 10 de En. de 2024
% Filter design
N = 10; % Filter order
Fcut = 2000; % Cut-off frequency of 2 kHz
Fs = 15000000; % Sampling frequency of 15 MHz
% Design a low-pass FIR filter
b = fir1(N, Fcut/(Fs/2));
a = 1; % For FIR filters, the denominator is 1
% Frequency range of interest
f_min = 0; % Minimum frequency in Hz
f_max = 2000; % Maximum frequency in Hz (around your signal frequency)
% Number of points to evaluate
n_points = 4096;
% Frequency vector (w radians/sample = f cycles/sec * (1/Fs) sec/sample * 2*pi rad/cycle)
% which is inverse of f = w * Fs / (2*pi) --- Thank you 'paul' for the
% sugguestion
w = linspace(0, f_max*2*pi/Fs, n_points);
% Calculate the frequency response
[h, w] = freqz(b, a, w);
% Convert rad/sample to Hz for plotting
f = w * Fs / (2*pi);
% Plot the magnitude response
figure;
plot(f, abs(h));
title('Frequency Response of the Designed FIR Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
%Optional: If you want to plot phase response, uncomment the following lines
figure;
plot(f, angle(h));
title('Phase Response of the Designed FIR Filter');
xlabel('Frequency (Hz)');
ylabel('Phase (Radians)');
grid on;
In this code, the fir1 function designs a 10th-order low-pass FIR filter with a cutoff frequency of 2 kHz, which should be suitable for your 1 kHz signal. The frequency response is plotted for frequencies up to 2 kHz, which provides a more focused view on the relevant part of the spectrum. Feel free to adjust the filter order (N) and the cutoff frequency (Fcut) as per your specific requirements.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
Feel free to contact me.
2 comentarios
Paul
el 10 de En. de 2024
Is this line correct?
% Frequency vector (in radians/sample)
w = linspace(0, f_max/(Fs/2), n_points);
Seems like it should be
% Frequency vector (w radians/sample = f cycles/sec * (1/Fs) sec/sample * 2*pi rad/cycle)
% which is inverse of f = w * Fs / (2*pi)
w = linspace(0, f_max*2*pi/Fs, n_points);
% Calculate the frequency response
[h, w] = freqz(b, a, w);
% Convert rad/sample to Hz for plotting
f = w * Fs / (2*pi);
Star Strider
el 10 de En. de 2024
You can set the 'XLim ' range for the subplot axes, however that may be a bit of a stretch even then. Another option is to signifficantly increase the size of the fft that is calculated, although that could take a shile to calculate.
Try this —
Fs = 15E+6;
Fn = Fs/2;
Wp = 1/Fn; % Passband Frequency (Normalised)
Ws = 2/Fn; % Stopband Frequency (Normalised)
Rp = 1; % Passband Ripple
Rs = 60; % Passband Ripple (Attenuation)
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Elliptic Order Calculation
[z,p,k] = ellip(n,Rp,Rs,Wp); % Elliptic Filter Design: Zero-Pole-Gain
[sos2,g2] = zp2sos(z,p,k); % Second-Order Section For Stability
figure
freqz(sos2, 2^16, Fs) % Filter Bode Plot
figure
freqz(sos2, 2^20, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 10])
set(subplot(2,1,2), 'XLim',[0 10])
figure
freqz(sos2, 2^24, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 5])
set(subplot(2,1,2), 'XLim',[0 5])
This uses a lowpass elliptic filter as the example. Narrow the 'XLim' values to your area-of-interest. Longer fft lengths than this are primarily a problem of available memory.
.
0 comentarios
Paul
el 10 de En. de 2024
If x is a digitalFilter object, then freqz can be called as
freqz(x,f,fs);
where f is vector of frequencies of interest to visualize the frequency response and fs is the sampiling frequency (15e6).
If x is not a digitalFilter object, then freqz(x) is not a valid, documented, call to freqz and you'll have to provide more information on what x is.
0 comentarios
Ver también
Categorías
Más información sobre Digital Filter Analysis en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!