I just ran both sets of code. The vectorized version ran much faster (seconds), of course, but I believe my suspicions that it won't work were true. The result is not the same and does not appear to be an effective filter. It has much more noise than the for loop version. I suspect it's essentially the same as if I used (1-alpha)*tsx.data(1:tslen-1,:) which is not the same as the equation I'm trying to achieve.
Vectorized or Optimized Finite Low Pass Filter
4 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
Honglei Chen
2014-7-8
编辑:Honglei Chen
2014-7-9
If your signal is uniformly spaced, and if I understand correctly, your alpha can be pre-determined, then this can be done via
y = filter([1 alpha-1],alpha,x)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Filter Design 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!