필터 지우기
필터 지우기

Removing spikey data from a sensor readout.

조회 수: 12 (최근 30일)
luc
luc 2015년 1월 28일
댓글: luc 2015년 4월 7일
I have a loop which reads out a sensor, once in a while the data is completely off, and gives a spike. I would like to remove those spikes.
The data given moves from -180 to 180 degrees, so when my measurement moves from -180 to 180 this should not be filtered (as this is normal).
However, if it moves from -180 to 90 then this counts as a spike.
It's not possible to do data processing after getting every readout, ideally there should be a filter of some sort in the loop itself.
Any ideas?

채택된 답변

Image Analyst
Image Analyst 2015년 1월 28일
Do you have the Signal Processing Toolbox? If so, you could use the median filter. If you want to replace spikes with the median value around them, then compute the median and subtract it from the original and take the absolute value. Then replace those elements with a high value with the median.
medianSignal = medfilt1(signal, 7);
diffSignal = abs(signal - medianSignal);
spikes = diffSignal > 100; % Whatever...
% Replace
fixedSignal = signal; % Initialize
fixedSignal(spikes) = medianSignal(spikes);
Or you can use the Savitzky-Golay filter, sgolayfilt(). A demo is attached.
If this is not right for you, then post your data - both a picture and the actual data in a txt or .mat file.
  댓글 수: 6
Image Analyst
Image Analyst 2015년 1월 30일
That's because you messed it up in several ways. You have y inside the loop and are trying to run sgolayfilt() on just a single value of y instead of the whole array. Don't use a loop. And you misused xPlot and yPlot by assigning an index, and you didn't add random numbers to each element. You added the same random number to all elements.
I fixed your code and include it here:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
% Make a noisy sine wave signal
x=1:100;
period = 50
y = sin(2*pi*x/period);
noiseAmplitude = 0.8;
y = y + noiseAmplitude * rand(size(y));
yplot=y;
xplot=x;
subplot(2,1,1);
plot(xplot, yplot, 'b-', 'LineWidth', 2);
grid on;
title('Noisy Signal', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Now smooth with a Savitzky-Golay sliding polynomial filter
windowWidth = 27
polynomialOrder = 3
smoothY = sgolayfilt(y, polynomialOrder, windowWidth);
subplot(2,1,2);
plot(x, smoothY, 'b-', 'LineWidth', 2);
grid on;
title('Smoothed Signal', 'FontSize', fontSize);
luc
luc 2015년 4월 7일
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Smoothing and Denoising에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by