Borrar filtros
Borrar filtros

Vectorized or Optimized Finite Low Pass Filter

2 visualizaciones (últimos 30 días)
Alan
Alan el 8 de Jul. de 2014
Comentada: Alan el 9 de Jul. de 2014
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 comentario
Alan
Alan el 8 de Jul. de 2014
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.

Iniciar sesión para comentar.

Respuesta aceptada

Honglei Chen
Honglei Chen el 8 de Jul. de 2014
Editada: Honglei Chen el 9 de Jul. de 2014
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)
  4 comentarios
Alan
Alan el 9 de Jul. de 2014
Correcting from 1-alpha to alpha-1 got rid of the 5X magnitude issue. However, this filter is actually increasing the noise in my signal, not decreasing. Perhaps I'm calculating my "alpha" wrong? Of course, I'm calculating "alpha" almost the same way in my for loop version - which actually filters it quite well but is just really slow. The one difference is I used an average value for dt (which is used to calculate alpha) across all for the filter() version and I update it each iteration in the for loop version. I wouldn't suspect the difference would be that great, though.
Alan
Alan el 9 de Jul. de 2014
It works. Sorry, I was not comparing the right graphs. Thank You very much! I marked it as accepted.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matched Filter and Ambiguity Function en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by