Smoothing a noisy signal

3 visualizzazioni (ultimi 30 giorni)
Anu
Anu il 25 Gen 2022
Risposto: KALYAN ACHARJYA il 26 Gen 2022
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

Risposte (1)

KALYAN ACHARJYA
KALYAN ACHARJYA il 26 Gen 2022
%% 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

Community Treasure Hunt

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

Start Hunting!

Translated by