フィルターのクリア

Smoothing a noisy data set

1 回表示 (過去 30 日間)
Bradley Baker
Bradley Baker 2018 年 12 月 9 日
回答済み: Astha Singh 2018 年 12 月 12 日
As a homework assignment, I need to make a function that uses a moving average to smooth a noisy data set.
The function I need to make takes a data set to be smooothed "y", the corresponding vector for the indipendent variable 't", as well as the number of data points used in the moving average "N" then return the sothed data set "ys" and a corresponidng time vecotr for the smoothed data "ts".
What I have so far:
function [ys,ts] = smoothN(y,t,N)
%smooothN.m uses a moving average to smooth data
% Input: y: data vector to be smoothed
% Input: t: the corresponding time vector to y
% Input: N: specifies how many data points to use when calculating the moving average
% Output: ys: the smoothed data vector
% Output: ts: the coresponding time vector
for i=1:length(y)
add=0;
for j=i:i+(N-1) % this for loop finds the summ of the elements within the moving avergae
add=add+y(i);
end
ys(i)=add/N; %finds the average and sets it into the smoothed vector
ts(i)=t(i); % adjusts the corresponding time vector
end
%Bradley
Unortunatly, this just returns the original vector and I can't figure out why.
Please help me

回答 (1 件)

Astha Singh
Astha Singh 2018 年 12 月 12 日
Hello,
The error is in variable indexing, in the second for-loop,
add = add+y(j)
instead of "add = add+y(i)".
Also, you would need to take care of the indexing of variable 'i' when the last 'N' sample values of 'ys' are being calculated.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by