Running data through a filter multiple times

조회 수: 7 (최근 30일)
Elliot Jones
Elliot Jones 2020년 6월 29일
답변: Star Strider 2020년 6월 29일
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