MATLAB Answers

Smoothing 2 columns text file

1 view (last 30 days)
Alex M.
Alex M. on 29 Nov 2016
Commented: Alex M. on 1 Dec 2016
Hi, I would like to have a smoothed line fitting my data in order to get rid of the noise. Below is the what the data looks like along with the code I am using. How can I implement a simple smoothing method to my code? Thank you.
fid=fopen('1GPL.txt');
s=textscan(fid,'%d %d %d','headerlines',23);
fclose(fid);
x=s{1};
y=s{2};
xx=x(x>=400 & x<=700);
yy=y(x>=400 & x<=700);
plot(xx,yy)

  1 Comment

Alex M.
Alex M. on 1 Dec 2016
In addition, I tried using smooth but it doesn't seem to be working properly here.

Sign in to comment.

Answers (1)

Jorrit Montijn
Jorrit Montijn on 1 Dec 2016
Hi Alex,
Have you looked into using a function like filtfilt()? You can look it up in the MATLAB help; you can use several built-in filters depending on the type of filtering you wish to apply.
Alternatively, you can perform a simple convolution with conv(). You could for example apply a Gaussian filter like this:
yyFiltered = conv(yy,normpdf(-2:2,0,1)./sum(normpdf(-2:2,0,1)),strFlag)
Note that this way you either get a shorter trace when strFlag is 'valid', or has artifacts near the edges, because of zero-padding when using strFlag = 'same'

  1 Comment

Alex M.
Alex M. on 1 Dec 2016
Hi Jorrit, I am unsure how to apply designfilt in my case when using filtfilt. Also the convolution method doesn't work and returns:
Undefined function 'conv2' for input arguments of type 'int32'.
Error in conv (line 40)
c = conv2(a(:),b(:),shape);
Error in test (line 12)
yyFiltered = conv(yy,normpdf(-2:2,0,1)./sum(normpdf(-2:2,0,1)),'valid')

Sign in to comment.

Sign in to answer this question.


Translated by