Using filterdesigner/FDAtool - setting parameters in the GUI and getting the parameters while my script is running
14 次查看(过去 30 天)
显示 更早的评论
Hello, so I've been trying to make a GUI that allows you to insert the stop frequency of a notch filter.
I want to use the fdatool but I'm not sure if the fdatool is able to get or set it's fpass1-fpass4 outside of the fdatool GUI.
In addition I need to get the coeffitients of the filter out of the fdatool after the user inserts the frequncy to filter.
Would like to know if there is a way to do so and if not how would you recommend me to create a notch filter without the filter designer tool.
(I dont want to work with the whole FFT of the signal because I want to create a causal filter).
Thanks for the answers.
2 个评论
Max Murphy
2019-12-30
Am I understanding correctly that you wish to import fpass1-fpass4 that are set in a separate GUI (which you made) into the fdatool GUI, then export the corresponding filter coefficients from fdatool? Is there a specific kind of filter design that you are going for outside of a generic notch filter (e.g. the other settings on the bottom left of the fdatool GUI)?
采纳的回答
Max Murphy
2019-12-30
编辑:Max Murphy
2019-12-30
In the fdatool GUI, select the "Bandstop" radio-button option in the Response Type panel to create a Notch filter. In the Design Method panel, click the radio-button option next to "FIR" and select "Window" from the drop-down menu. This will give you the option to select Kaiser for the Window type in the Options panel.
In the Filter Order panel, you should specify a fixed order if you already know that you have a specific filter order you must use based on your application's constraints. Otherwise, select the "Minimum order" radio button option from the Filter Order panel.
Next, you need to input the frequency constraints to the Frequency Specifications panel. In both cases, Fs is the sample rate that is used to acquire the signal you are looking at. The other parameters will depend on what you selected for the Filter Order:
- For a fixed filter order the filter will be designed such that the attenuation at Fc1 ("start" of notch) and Fc2 ("end" of notch) will be 6 dB (these are in the Frequency Specifications panel). If you make the notch very narrow while constraining the filter order, then this may only be realized with a very "strange" passband, so keep that in mind.
- If you select "Minimum order" and give very "difficult" filter constraints, you may end up with an extremely long (and essentially impractical) filter, so keep that in mind as well. With "Minimum Order" selected, you have Fpass1, which specifies the frequency where the lower part of the "passband" ends (the start of the transition from "pass" to "stop" bands). Fstop1 is the end of this first transition. Fstop2 is the start of the transition from stop band back into pass band, and Fpass2 is where the second transition ends. In your 60 Hz example, This could be Fpass1 = 45, Fstop1 = 49, Fstop2 = 61, Fpass2 = 75, which puts 60 Hz in the middle of your stop band where the maximum attenuation would occur. Using these specifications for a signal sampled at 1 kHz and stopband attenuation (Astop) = 30 dB in the Magnitude Specifications panel, you get a filter order of 114, which is probably much higher than you would like for causal applications.
However, if that is suitable for your application, you could then make it easier to customize in the future by going to File in the GUI menu bar at the top, click Generate MATLAB Code, and click Filter Design Function. The generated function exported according to the example I gave above is shown here:
function Hd = notch_filter_60Hz
%NOTCH_FILTER_60HZ Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.2 and the DSP System Toolbox 9.4.
% Generated on: 30-Dec-2019 10:11:53
% FIR Window Bandstop filter designed using the FIR1 function.
% All frequency values are in Hz.
Fs = 1000; % Sampling Frequency
Fpass1 = 45; % First Passband Frequency
Fstop1 = 59; % First Stopband Frequency
Fstop2 = 61; % Second Stopband Frequency
Fpass2 = 75; % Second Passband Frequency
Dpass1 = 0.028774368332; % First Passband Ripple
Dstop = 0.031622776602; % Stopband Attenuation
Dpass2 = 0.057501127785; % Second Passband Ripple
flag = 'scale'; % Sampling Flag
% Calculate the order from the parameters using KAISERORD.
[N,Wn,BETA,TYPE] = kaiserord([Fpass1 Fstop1 Fstop2 Fpass2]/(Fs/2), [1 ...
0 1], [Dpass1 Dstop Dpass2]);
% Calculate the coefficients using the FIR1 function.
b = fir1(N, Wn, TYPE, kaiser(N+1, BETA), flag);
Hd = dfilt.dffir(b);
% [EOF]
Now, I can just call that function any time to get the filter object, Hd.
To address your need to enter in elements in a function as desired to generate an output filter, you could do something like:
function Hd = notch_filter_60Hz(varargin)
%NOTCH_FILTER_60HZ Returns a discrete-time filter object.
%
% Hd = notch_filter_60Hz; Return default Hd
% Hd = notch_filter_60Hz('paramname',value,...); (case-sensitive)
% --> e.g:
% >> Hd = notch_filter_60Hz('Fs',24414.0625,'Fpass1',40,...);
% MATLAB Code
% Generated by MATLAB(R) 9.2 and the DSP System Toolbox 9.4.
% Generated on: 30-Dec-2019 10:11:53
% FIR Window Bandstop filter designed using the FIR1 function.
% All frequency values are in Hz.
p = struct; % "Default values" struct
p.Fs = 1000; % Sampling Frequency
p.Fpass1 = 45; % First Passband Frequency
p.Fstop1 = 59; % First Stopband Frequency
p.Fstop2 = 61; % Second Stopband Frequency
p.Fpass2 = 75; % Second Passband Frequency
p.Dpass1 = 0.028774368332; % First Passband Ripple
p.Dstop = 0.031622776602; % Stopband Attenuation
p.Dpass2 = 0.057501127785; % Second Passband Ripple
p.flag = 'scale'; % Sampling Flag
% Parse any optional 'Name', value input pairs
for iV = 1:2:numel(varargin)
if isfield(p,varargin{iV})
p.(varargin{iV}) = varargin{iV+1};
end
end
% Calculate the order from the parameters using KAISERORD.
[N,Wn,BETA,TYPE] = kaiserord([p.Fpass1 p.Fstop1 p.Fstop2 p.Fpass2]/(p.Fs/2), [1 ...
0 1], [p.Dpass1 p.Dstop p.Dpass2]);
% Calculate the coefficients using the FIR1 function.
b = fir1(N, Wn, TYPE, kaiser(N+1, BETA), p.flag);
Hd = dfilt.dffir(b);
% [EOF]
This modified version allows you to enter input argument pairs that modify the "default" parameters you have generated using the fdatool originally. I think this is closer to what you want. Then from your custom GUI, you can enter whatever values you would like and get them to go into this generated function, which returns the filter object.
Note that to use the FIR filter you've generated, it again depends on your application. If you would like to see the phase offset induced by your filter, then use filter:
in = load('data.mat','x');
y = filter(Hd,in.x);
If you do not want to induce a phase offset, then use filtfilt:
in = load('data.mat','x');
b = Hd.Numerator;
a = 1;
y = filfilt(b,a,in.x); % just want to remove 60 Hz noise for example
As a final note, you may consider using an IIR filter because to achieve comparable attenuation you can do it with a lower order (since you mentioned wanting a causal filter specifically). The tradeoff is that if you have very "difficult" constraints, an IIR filter can become unstable, so keep that in mind. In practice you can implement a causal state filter by initializing the "future" values at the start to be zero (or some informed prior value).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Filter Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!