Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to smoothen a plot in MATLAB?

Tags:

I have some 9000 points that are plotted on a graph:

[Full resolution]

alt text

Actually, the plot is not as smooth as I wanted it to be. Is there some way I can smoothen the graph to a required degree?

Or some form of thresholding so that I can selectively smoothen out the parts that is too bumpy?

I am not sure but can fast-fourier-transform help?

like image 850
Lazer Avatar asked Oct 04 '09 09:10

Lazer


People also ask

How do you improve the quality of a plot in Matlab?

It is possible to increase the resolution of the plots obtained in MATLAB. On the Plot window ---- Click on <Edit> --> <Figure properties> --> <Export setup> --> <Rendering>. Then change the resolution to 300 or 600dpi.

How do I smooth 2d data in Matlab?

You can use convolution to smooth 2-D data that contains high-frequency components. Create 2-D data using the peaks function, and plot the data at various contour levels. Inject random noise into the data and plot the noisy contours.

How can I smooth my noisy data?

Data smoothing can be defined as a statistical approach of eliminating outliers from datasets to make the patterns more noticeable. The random method, simple moving average, random walk, simple exponential, and exponential moving average are some of the methods used for data smoothing.


2 Answers

If you have the Curve Fitting Toolbox, you can use the smooth function. The default method is a moving average of size 5 (method can be changed). An example:

% some noisy signal
Fs = 200; f = 5;
t = 0:1/Fs:1-1/Fs;
y = sin(2*pi*f*t) + 0.6*randn(size(t));
subplot(411)
plot(y), title('Noisy signal')

% smoothed signal
subplot(412)
plot( smooth(y, 5, 'moving') ), title('smooth')
ylim([-2 2])

If not, you can use use your own window function using the filter function from core MATLAB:

% equivalent to a moving average window
wndwSize = 5;
h = ones(1,wndwSize)/wndwSize;
subplot(413)
plot( filter(h, 1, y) ), title('filter + square window')

% Guassian
h = pdf('Normal',-floor(wndwSize/2):floor(wndwSize/2),0,1);
subplot(414)
plot( filter(h, 1, y) ), title('filter + Guassian window')

screenshot

like image 54
Amro Avatar answered Oct 30 '22 22:10

Amro


A simple (ad hoc) way is to just take a weighted average (tunable by alpha) at each point with its neighbors:

data(2:n-1) = alpha*data(2:n-1) + (1-alpha)*0.5*(data(1:n-2)+data(3:n))

or some variation thereof. Yes, to be more sophisticated you can Fourier transform your data first, then cut off the high frequencies. Something like:

f = fft(data)
f(n/2+1-20:n/2+20) = zeros(40,1)
smoothed = real(ifft(f))

This cuts out the highest 20 frequencies. Be careful to cut them out symmetrically otherwise the inverse transform is no longer real. You need to carefully choose the cutoff frequency for the right level of smoothing. This is a very simple kind of filtering (box filtering in frequency domain), so you can try gently attenuating high order frequencies if the distortion is unacceptable.

like image 37
Victor Liu Avatar answered Oct 30 '22 20:10

Victor Liu