Read values from fvtool graph

조회 수: 6 (최근 30일)
Gabor
Gabor 2025년 3월 24일
댓글: Mathieu NOE 2025년 4월 7일
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');

채택된 답변

Mathieu NOE
Mathieu NOE 2025년 3월 25일
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
Gabor
Gabor 2025년 4월 5일
I did, thanks for the notification.
Mathieu NOE
Mathieu NOE 2025년 4월 7일
thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Filter Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by