Implementación de Matlab y luego c: datos suaves

I have some data that is slightly noisy. I want to clean it as well as the derivative. A very simple "filter" such as Y(n)=0.2*X(n)+0.8*Y(n-1) suits me well: in other words, the output is smooth enough. Here is an example with both data and its derivative:

Top: data, Bottom: derivative of data

Nevertheless, as it's real-time, I cannot accept a big lag in data. For the derivative, the lag is around 10 iterations: 5 iterations (to get the filtered data) and then another 5 iterations (to get the filtered derivative data).

How can I implement an algorithm in Matlab - in fine, I need a c implementation - that produces a similar result but with very little lag, such as a maximum of 3 iterations EVEN and ESPECIALLY for the derivative.

Aquí está el código de Matlab:

%Here is data input;
data = [5554 5767 5737 5828 6011 6011 6103 6225 6408 6286 6347 6439 6591 6683 6530 6652 6805 7019 7080 7019 6988 6988 6958 6958 6927 6896 6866 6805 6835 6713 6591 6713 6713 6652 6713 6713 6835 6896 6744 6622 6683 6561 6591 6591 6622 6561 6500 6652 6774 6805 6958 7049 7171 7232 7293 7293 7415 7476 7690 8087 8056 7965 8178 8270 8148 8178 8392 8239 8056 8026 7873 7873 7476 7446 7476 7293 7171 7141 7263 7019 7141 6774 6652 6530 6530 6469 6561 6622 6652 6713 6866 6958 6958 7110 6958 7049 6805 6866 6866 6744 7049 6805 6652 6530 6622 6744 6408 6652 6683 6713 6713 6622 6744 6805 6713 6683 6805 6835 6927 6866 6805 6744 7049 7232 7171 7141 7202 7354 7324 7415 7415 7415 7965 8178 8178 8178 8178 7995 7995 7965 7843 7904 7904 7812 7812 7720 7690 7843 7873 7904 8056 7904 7843 7934 7873 7873 7781 7781 7873 7873 7873 7690 7659 7690 7720 7751 7659 7598 7629 7659 7568 7537 7629 7537 7659 7568 7568 7476 7568 7598 7354 7324 7476 7354 7385 7202 7293 7293 6958 6866 6866 6683 6927 6927 6927 7019 7019 7049 7141 7202 7293 7385 7568 7446 7446 7415 7354 7263 7293 7202 7202 7202 7232 7202 7263 7141 7171 7019 7141 7232 7080 7232 7141 7263 6958 7019 7019 6866 6896 6744 6774 6805 6835 6866 6774 6744 6774 6744 6744 6866 6958 6958 7019 6988 6988 6896 6958 7080 7080 7171 7232 7202 7354 7385 7385 7476 7720 7690 7598 7659 7659 7690 7720 7720 7690 7690 7781 7751 7659 7812 7873 7659 7629 7446 7446 7629 7354 7415 7476 7568 7415 7385 7385 7324 7202 7324 7110 6958 7019 7019 7049 6774 7202 6561 6408 6195 5920 5920 5920 5859 5615 5645 5584 5676 5432 5401 5126 5096 5004 5004 4791 4852 4791 4791 4730 4760 4730 4730 4699 4760 4669 4730 4760 4760 4791 4882 4882 4882 4974 4943 4974 5065 5096 5157 5218 5279 5279]';

%Define smooth coefficient
c=0.2;

%Filter data
data_filtered=zeros(size(data),1);
for n=drange(2:size(data))
    data_filtered(n,1)=c*data(n)+(1-c)*data_filtered(n-1,1);
end

%Graph both data and filtered data
subplot(2,1,1);
plot([1:size(data)], data, [1:size(data)], data_filtered);

%Calculate derivative data. Delta time is constant.
for n=drange(2:size(data_filtered))
    data_derivative(n,1)=data(n)-data(n-1,1);
end

%Calculate derivative based on filtered data. Delta time is constant.
for n=drange(2:size(data_filtered))
    data_filtered_derivative(n,1)=data_filtered(n)-data_filtered(n-1,1);
end

%Filter derivative (which is based on filtered data)
data_filtered_derivative_filtered=data_filtered_derivative;
for n=drange(2:size(data_filtered_derivative))
    data_filtered_derivative_filtered(n,1)=c*data_filtered_derivative(n)+(1-c)*data_filtered_derivative_filtered(n-1,1);
end

%Graph both derivative data and filtered derivative data
subplot(2,1,2);
plot([1:size(data_derivative)], data_derivative, [1:size(data_derivative)], data_filtered_derivative_filtered);

preguntado el 09 de septiembre de 13 a las 21:09

What do you mean with iterations? Number of time-steps of your real-time algorithm? I mean, are you limited by calculation time, or by the 'group delay' if your digital filters? -

We are not talking about calculation limitation here. On the first graph, you can clearly see that the green curve is shifted to the right compared to the blue curve. It's what I want to avoid as much as possible. I guess that some people will suggest a Kalman filter but still my experiment shows that there is some big delay. -

I am not a filter expert, but I think this delay is something fundamental. You are doing something in real-time, so you cannot do tricks like filtfilt does by applying the same filter forwards and backwards to avoid delays. The delay probably scales inversely with the cut-off frequency of your low-pass, so try to increase the cutoff frequency as much as possible. Finally note that a first-order low-pass filter followed by a derivator is effectively a first-order high-pass filter. Maybe you could reduce the delay by implementing that in a single IIR or so, instead of doing it in two steps. -

@Bas Swinckels: Thanks. That's exactly the point: you can't use data in the future with a filter forwards/backwards as it's real-time. I'm just trying to minimize as much as possible the delay and still get some smooth data. -

1 Respuestas

If you want an excellent, low-lag filter it seems something like Jurik JMA might be for you. It is probably pretty expensive (and definitely not free) but the paper might give you ideas on other (possibly free) algorithms.

I have not tested that algorithm, I have just found it and liked the pictures.

Respondido el 14 de Septiembre de 13 a las 12:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.