Running data through a filter multiple times

7 次查看(过去 30 天)
I'm trying write a function that will run data through a chebyshev type 2 filter a number of times. I currently have written this which doesn't work and I'm currently struggling with how to fix it or wether I'm attacking this completely wrong. To build the filter im using [bd,ad]=cheby2(N,R,Wst). I also need to have each stage of the dataout saved.
function [datafiltered]casfilter(bd,ad,data,nfilters)
% CASFILTER - pass data through a filter defined bd and ad n number of
% times and give datafiltered. n>1.
%
% useage: [datafiltered]=casfilter(bd,ad,data,nfilters)
%
% Elliot Jones, 21st June 2020
[dataout1,zout1]=filter(bd,ad,data);
for n=1:(nfilters-1)
[dataout"n+1",zout"n+1"]=filter(bd,ad,dataout"n",zout"n");
end
dataout"n"=datafiltered
end

采纳的回答

Star Strider
Star Strider 2020-6-29
It is not easy to follow what you are doing here, especially with respect to your using the strings as part of the subscript (that will absolutely not work).
I would do something like this:
dataout(:,1) = filtfilt(bd,ad,data(:)); % Convert To Column Vector, Then Filter
for n=2:nfilters
dataout(:,n) = filtfilt(bd,ad,dataout(:,n-1)); % Store Sucessive Column Vectors
end
datafiltered = dataout; % Define ‘datafiltered’ As ‘dataout’ Matrix
If you do not want to save the entire matrix of intermediate results, do not subscript them.
If you only want to output the final result, do this instead:
datafiltered = dataout(:,end); % Define :datafiltered, As Last Column Of 'dataput' Matrix
Make appropriate changes to get the result you want.
See the documentation section on: Matrix Indexing to understand how to do it correctly.
Also consider preallocating the matrix, so before the loop set it to:
dataout = zeros(numel(data), nfilters);
to execute the loop faster.
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matched Filter and Ambiguity Function 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by