Smoothing a noisy signal

조회 수: 3 (최근 30일)
Anu
Anu 2022년 1월 25일
답변: KALYAN ACHARJYA 2022년 1월 26일
I want to smooth my noisy data using the moving average. I also want to vary the rolling window and check how different rolling windows smooth my noisy data. I have written the code for a dummy sinusoidal signal. The only problem is that my data after each rolling window gets overwritten. I understand why it is but don't know how to fix it. May I request you to please suggest how to fix it?
%% how to create a noisy data
x = 0:0.1:20; %creating a dummy data
y = sin(x); %finding a sine of the variable x
n = randn(1, length(x)); % generating a random (Gaussian) noise of size 1x length(x)
y_noisy = y+n; % creating the sine function noisy by adding the Gaussian noise
%% how to smooth the noisy data wout using any filter but using moving average
for rollingwindow = [2,20] % specify the rolling averaging window
N = length(y_noisy);
m = N - rollingwindow + 1;
newy_noisy =zeros(m,1); % initializing the matrix
for i = 1:m
newy_noisy(i) = mean(y_noisy(i:i+(rollingwindow-1)));
end
%% here newy_noisy has to be defined in such a way it is not overwritten. But how?
figure();
plot(y, 'b');
hold on
plot(y_noisy, 'r')
hold on
plot(newy_noisy, 'k', 'linewidth', 1)
legend('theoretical','noisy','smooth')
title('noisy vs smooth', sprintf('rollwindow = %d', rollingwindow));
outputFileName = sprintf('rollwin%d', rollingwindow);
outputFullFileName = fullfile('C:\Users\anusu\Desktop',outputFileName);
saveas(gcf, outputFullFileName, 'png');
hold off
end

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2022년 1월 26일
%% how to create a noisy data
x = 0:0.1:20; %creating a dummy data
y = sin(x); %finding a sine of the variable x
n = randn(1, length(x)); % generating a random (Gaussian) noise of size 1x length(x)
y_noisy = y+n; % creating the sine function noisy by adding the Gaussian noise
%% how to smooth the noisy data wout using any filter but using moving average
for rollingwindow=2:20 % specify the rolling averaging window
N = length(y_noisy);
m = N - rollingwindow + 1;
newy_noisy =zeros(m,1); % initializing the matrix
for i = 1:m
newy_noisy(i) = mean(y_noisy(i:i+(rollingwindow-1)));
end
%% here newy_noisy has to be defined in such a way it is not overwritten. But how?
figure();
plot(y, 'b');
hold on
plot(y_noisy, 'r')
hold on
plot(newy_noisy, 'k', 'linewidth', 1)
legend('theoretical','noisy','smooth')
title(['noisy vs smooth', sprintf('rollwindow = %d', rollingwindow)]);
outputFileName = sprintf('rollwin%d', rollingwindow);
outputFullFileName = fullfile('C:\Users\anusu\Desktop',outputFileName);
%saveas(gcf, outputFullFileName, 'png');
hold off
end
Can you look for this? If not, specify with more details

카테고리

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