Vectorized or Optimized Finite Low Pass Filter
Ältere Kommentare anzeigen
Let me start out with I do not have the signal processing toolbox. A simple low pass filter function should be easy. Indeed, "brute force" it is, I have the following code for filtering a timeseries:
function tsy = dlpf( tsx, lpff )
%dlpf - discrete low pass filter
% Outputs a low pass filtered timeseries when a timeseries and frequency
% are entered
tslen = length(tsx.time);
tau = 1/(2*pi*lpff);
%Line by line version
%This is more accurate, but much slower
disp = '';
tsy = tsx;
for n = 2:tslen
dt = tsx.time(n) - tsx.time(n-1);
alpha = dt/(tau+dt);
tsy.data(n,:) = alpha*tsx.data(n,:) + (1-alpha)*tsy.data(n-1,:);
end
end
But, I feel like I've given up whenever I use a for loop in MATLAB. I have timeseries data with over 400000 entries and as with any for loop in MATLAB, this is VERY slow. I want to make a "fast" version that's vectorized, or at least optimized in some way. The problem is, I don't think the equation:
y[i] = a*x[i] + (1-a) * y[i-1]
can be vectorized since the series y appears on both sides of the equation. If I did vectorize, it would look something like this:
%Vectorized version
%For speed, this takes an average dt, which will work if the timeseries
%is more or less uniform.
%For a very nonuniform timeseries, a line by line version would be
%needed
dt = mean(tsx.time(2:tslen)-tsx.time(1:tslen-1));
alpha = dt/(tau+dt);
tsy = tsx
tsy.data(2:tslen) = (1-alpha)*tsy.data(1:tslen-1,:) + alpha*tsx.data(2:tslen)
I fear that the tsy.data series on the right will not contain the correct values, as they would be updated with each iteration through the series. I don't know how MATLAB would handle this in the background. Would this do what I want, or does anyone know of a better way to do this?
1 Kommentar
Alan
am 8 Jul. 2014
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Surrogate Optimization finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!