Read values from fvtool graph

Hi all,
I have this script, for IIR filter.
I would like to get to know the points where the graph reaches the -6dB points at the Magnitude Response.
Any help and solution is appreciated.
Bests, Gabor
clc; clear; close all;
% Parameters
Fs = 44100; % Sampling frequency (Hz)
Fc1 = 15.75; % Lower cutoff frequency (Hz)
Fc2 = 47.25; % Upper cutoff frequency (Hz)
Q = 1; % Quality factor
% IIR Biquad bandpass filter design
d = designfilt('bandpassiir', ...
'FilterOrder', 2, ... % Second-order (biquad)
'HalfPowerFrequency1', Fc1, ...
'HalfPowerFrequency2', Fc2, ...
'SampleRate', Fs);
% Frequency response visualization
fvtool(d)
% Test signal generation (sine waves at different frequencies)
t = 0:1/Fs:1;
x = sin(2*pi*50*t) + sin(2*pi*200*t) + sin(2*pi*400*t); % 50 Hz, 200 Hz, and 400 Hz
% Apply filtering
y = filter(d, x);
% Time-domain display
figure;
subplot(2,1,1);
plot(t, x);
title('Input signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(t, y);
title('Filtered signal');
xlabel('Time (s)');
ylabel('Amplitude');

 采纳的回答

hello
I am not sure you can retrieve the data (mag,phase) from fvtool object , but you can use freqz instead
now both code below and the fvtool plot seems to indicate different cut off frequencies vs what you asked for in designfilt (I rarelly use it so there might be an explanantion I am not aware off)
plot from fvtool :
try this
% Parameters
Fs = 44100; % Sampling frequency (Hz)
Fc1 = 15.75; % Lower cutoff frequency (Hz)
Fc2 = 47.25; % Upper cutoff frequency (Hz)
Q = 1; % Quality factor
% IIR Biquad bandpass filter design
d = designfilt('bandpassiir', ...
'FilterOrder', 2, ... % Second-order (biquad)
'HalfPowerFrequency1', Fc1, ...
'HalfPowerFrequency2', Fc2, ...
'SampleRate', Fs);
% Frequency response visualization
freq = logspace(0,3,1000);
h =freqz(d.Numerator,d.Denominator,freq,Fs);
mag_dB = 20*log10(abs(h));
[f_6db_low,f_6db_up] = find_zc(freq,mag_dB,-6) % find both -6 dB cut off frequencies
f_6db_low = 11.3250
f_6db_up = 65.7123
figure,
semilogx(freq,mag_dB)
hold on
plot(f_6db_low,-6,'r+');
plot(f_6db_up,-6,'b+');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% put data in rows
x = x(:);
y = y(:);
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end

8 个评论

% % Alternative code :
% Parameters
Fs = 44100; % Sampling frequency (Hz)
Fc1 = 15.75; % Lower cutoff frequency (Hz)
Fc2 = 47.25; % Upper cutoff frequency (Hz)
% IIR Butterworth bandpass filter design
%It is best not to use the transfer function implementation of discrete (digital) filters,
%because those are frequently unstable.
%Use zero-pole-gain and second-order-section implementation instead.
%Example :
[z,p,k] = butter(2,[Fc1 Fc2]/(Fs/2));
[sos1] = zp2sos(z,p,k);
% Frequency response visualization
freq = logspace(0,3,1000);
h =freqz(sos1,freq,Fs);
mag_dB = 20*log10(abs(h));
[f_6db_low,f_6db_up] = find_zc(freq,mag_dB,-6) % find both -6 dB cut off frequencies
f_6db_low = 13.5462
f_6db_up = 54.9371
figure,
semilogx(freq,mag_dB)
hold on
plot(f_6db_low,-6,'r+','markersize',15);
plot(f_6db_up,-6,'b+','markersize',15);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% put data in rows
x = x(:);
y = y(:);
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
well , I just remember this morning that "half power" cut off frequency correspond to -3dB magnitude points - so there is in fact no surprise that -6dB and -3dB cut off frequencies are not equal !
so I have already given you the method for the -6 dB frequencies , but just for my own fun let's verify the -3dB frequencies (results should match the specified cut off freqs) :
(and fortunately it's matching !!!)
% Parameters
Fs = 44100; % Sampling frequency (Hz)
Fc1 = 15.75; % Lower cutoff frequency (Hz)
Fc2 = 47.25; % Upper cutoff frequency (Hz)
Q = 1; % Quality factor
% IIR Biquad bandpass filter design
d = designfilt('bandpassiir', ...
'FilterOrder', 2, ... % Second-order (biquad)
'HalfPowerFrequency1', Fc1, ...
'HalfPowerFrequency2', Fc2, ...
'SampleRate', Fs);
% Frequency response visualization
freq = logspace(0,3,1000);
h =freqz(d.Numerator,d.Denominator,freq,Fs);
mag_dB = 20*log10(abs(h));
[f_3db_low,f_3db_up] = find_zc(freq,mag_dB,-3) % find both -3 dB cut off frequencies
f_3db_low = 15.7688
f_3db_up = 47.1941
figure,
semilogx(freq,mag_dB)
hold on
plot(f_3db_low,-3,'r+');
plot(f_3db_up,-3,'b+');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% put data in rows
x = x(:);
y = y(:);
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
same test for the second code :
% Parameters
Fs = 44100; % Sampling frequency (Hz)
Fc1 = 15.75; % Lower cutoff frequency (Hz)
Fc2 = 47.25; % Upper cutoff frequency (Hz)
% IIR Butterworth bandpass filter design
%It is best not to use the transfer function implementation of discrete (digital) filters,
%because those are frequently unstable.
%Use zero-pole-gain and second-order-section implementation instead.
%Example :
[z,p,k] = butter(2,[Fc1 Fc2]/(Fs/2));
[sos1] = zp2sos(z,p,k);
% Frequency response visualization
freq = logspace(0,3,1000);
h =freqz(sos1,freq,Fs);
mag_dB = 20*log10(abs(h));
[f_3db_low,f_3db_up] = find_zc(freq,mag_dB,-3) % find both -3 dB cut off frequencies
f_3db_low = 15.7596
f_3db_up = 47.2215
figure,
semilogx(freq,mag_dB)
hold on
plot(f_3db_low,-3,'r+');
plot(f_3db_up,-3,'b+');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% put data in rows
x = x(:);
y = y(:);
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
hello again
problem solved ?
Hi, thanks for the help.
It solved my issue!
hello
glad it helped
do you mind accepting my answer ? thanks !
I did, thanks for the notification.

请先登录,再进行评论。

更多回答(0 个)

类别

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by