I can remove the noise using moving average filter. The MATLAB code is shown bellow.
But how can I remove the noise using mean and meadian filters?
Signal Processing Stack Exchange is a question and answer site for practitioners of the art and science of signal, image and video processing. It only takes a minute to sign up.Sign up to join this community
You do not need loops. This implements a moving average filter, a median filter, and a median filter followed by a moving average:
%x = noise1; x = randn(1,2000)+1; lengthMean = 5; lengthMedian = 7; xFiltMedian = medfilt1(x,lengthMedian); xFiltMean = filter(ones(lengthMean,1)/lengthMean,1,x); xFiltMedianMean = filter(ones(lengthMean,1)/lengthMean,1,xFiltMedian); figure(1);clf;hold on plot(x(500:1500),'b'); plot(xFiltMedian(500:1500),'r'); plot(xFiltMean(500:1500),'g'); plot(xFiltMedianMean(500:1500),'c');
As said by @jojek the median filter is often more efficient than an averaging filter for spikes and outliers. And if you choose to apply both filters, it is generally more useful to apply the median first (as done in the code above).
The choice of filter length should normally be related to properties of your signal and the noise (spectrum, distribution), and to the goal you are assigned with. But the length of the signal is usually not a criterion. For symmetry properties, odd-length filters are practical.
Without further details, you can start with longer filters (e.g.
lengthMedian= 37), see how they perform (how they reduce the noise but also degrade the signal's properties), and reduce the length progressively to the equilibrium point, where shorter filters do not yield better results.
However, the noise in the valley suggests it is not stationary, so you may need more adaptive filters (e.g. with a varying length), or a little more involved like a weigthed median filter that limits the location and flattening issues you get with long median filters. It is discussed on SE Cross validated.