run.avg: Perform Running Average via Convolution

Description Usage Arguments Details Value Note Author(s) See Also Examples

View source: R/run.avg.r

Description

Smooth input data by convolution it with with a boxcar function of specified width. This is done in the frequency domain using multiplication.

Usage

1
run.avg(data, win)

Arguments

data

signal (vector) to convolve.

win

width of the boxcar in samples.

Details

Convolution occurs in the frequency domain via a multiplication.

Value

Smoothed vector with the same dimension as data

Note

This function uses fft which can take significantly long run times if the input signal is prime or is divisible by few integers.

Author(s)

Alex J.C. Witsil

See Also

fft fftshift

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
## make a delta 2D (time series) function
delta <- rep(0,101)
delta[floor(length(delta)/2)] = 1

## define a window length to average over
win = 20

## filter the delta function...this will result in a boxcar
box.car <- run.avg(delta,win)

## note sum(box.car) should equal the sum of the original signal...
## in this case sum(box.car)==1

##############
## PLOTTING ##
##############

plot(delta,type='h',lwd=2)
lines(box.car,col='blue',lwd=2,type='h')

legend('topright',col=c('black','blue'),legend=c('delta','running average'),lwd=2)

imagefx documentation built on Feb. 14, 2020, 1:07 a.m.