Moving average: Create a function that takes a signal vector as input and computes the five-sample weighted moving average of the signal.

4 vues (au cours des 30 derniers jours)
Hi, I am stuck on the following assignment which I can't seem to understand at all. I'm at a loss for how to begin and approach this problem. Help is greatly appreciated! Thanks!
Assignment
When recording any type of noisy data, that be audio signals or stock prices, noise is often reduced by smoothening the data. One algorithm of smoothing a signal is the n-sample symmetric weighted moving average. Let us say we are given a signal as a vector y with values yi for i ∈ (1, . . . , N). Then, a five-sample symmetric weighted moving average, yˆ, can be computed by
yˆi = yi−2 + 2 · yi−1 + 3 · yi + 2 · yi+1 + yi+2 9 , (6.8)
i.e., as a weighted average of the surrounding datapoints.
To compute the first two and last two smoothened signal values, we must make some assumption about the signal outside its bounds:
We will assume that yi is zero for i < 1 and i > N. The figure shows a noisy measurement of a sine wave function and a five-sample symmetric weighted moving average.
Create a function that takes a signal vector as input and computes the five-sample weighted moving average of the signal.
Sample template
function ysmooth = movingAvg(y) % Insert your code here.

Réponses (1)

Image Analyst
Image Analyst le 17 Mar 2022
You can use conv() or movmean() inside your function, if they will allow you to use built-in functions.
function ysmooth = movingAvg(y)
ysmooth = movmean(%Insert your code
Otherwise use a for loop
function ysmooth = movingAvg(y)
for k = 1 : length(y) - 5
yInMovingWindow = y(%Insert your code
ysmooth(k) = mean(%Insert your code
end

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by