To smooth the noisy signal with integral

14 views (last 30 days)
Ki
Ki on 2 Dec 2020
Answered: Star Strider on 2 Dec 2020
Hi all,
I am trying to learn the signal progressing with MATLAB. I have a nosiy signal with frequency of f=1/20 Hz, it takes 1 minute to collect one sample and so the signal along with noise as
f = 1/20;
t=0:0.2:50;
noise = rand(size(t));
y = sin (2*pi*f*t) + noise;
I need to smooth the signal. I read a book about signal process and it integrate the signal within certain time for the purpose. I assume I need to smooth the signal every 4 samples, is the following code correct to do the job?
Y = zeros(size(y)); % the signal after smooth
len = 4; % smooth every 4 samples
for n=1:length(y)
if (n>len)
Y(n) = trapz(y(n-len:n))/len;
end
end
I got the code by trail and error. The plots looks ok. But after I read the article for numerical integral, I find that we need to multiple the time difference Delta t=0.2 to the trapz to get the correct intergral, that is
for n=1:length(y)
if (n>len)
Y(n) = trapz(y(n-len:n))*0.2/len;
end
end
But to do so, the amplitude of the smoothed signal is pretty off, am I missing something here? Thanks.

Answers (1)

Star Strider
Star Strider on 2 Dec 2020
I believe you have the correct approach.
I would do something like this:
f = 1/20;
t=0:0.2:50;
noise = randn(size(t));
y = sin (2*pi*f*t) + noise;
Y = zeros(size(y)); % the signal after smooth
len = 4; % smooth every 4 samples
for n=1:length(y)-len
if (n>len)
Y(n) = trapz(y(n:n+len))/len;
end
end
figure
plot(t, y)
hold on
plot(t, Y, 'LineWidth',1.5)
hold off
grid
Note that I use randn here instead of rand. The randn function produces Gaussian-distributed (normally-distributed) random numbers with a mean of 0 and a standard deviation of 1. The rand function produces uniformly-distributed random numbers going from 0 to 1, with a mean of 0.5. This may be the source of the amplitude deviation you were seeing, since the mean would integrate, creating a positive slope instead of the 0 slope created by using randn (that approximates real white noise).
.

Community Treasure Hunt

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

Start Hunting!