Find the moving-average of a vector without using a `for`

loop.

Create a 1-by-100 row vector of sinusoidal input data corrupted by random noise. Initialize the random number generator to make the output of `rand`

repeatable.

A moving-average filter is represented by the following difference equation,

Define the numerator coefficients of the rational transfer function. Use a window size of 5.

b =
0.2000 0.2000 0.2000 0.2000 0.2000

Define the denominator coefficients of the rational transfer function.

Find the moving-average of the data with a window size of 5.

`y(1)`

is equivalent to `0.2*x(1)`

.

`y(2)`

is equivalent to `0.2*(x(1)+x(2))`

.

...

`y(5)`

is equivalent to `0.2*(sum(x(1:5)) = mean(x(1:5))`

.

`y(6)`

is equivalent to `mean(x(2:6))`

.

...

`y(100)`

is equivalent to `mean(x(96:100))`

.

Compare the original data and the filtered data using an overlaid plot.

Filter the rows or columns of a matrix with the following rational transfer function,

Create a 15-by-2 matrix of random input data. Initialize the random number generator to make the output of `rand`

repeatable.

x =
0.8147 0.1419
0.9058 0.4218
0.1270 0.9157
0.9134 0.7922
0.6324 0.9595
0.0975 0.6557
0.2785 0.0357
0.5469 0.8491
0.9575 0.9340
0.9649 0.6787
0.1576 0.7577
0.9706 0.7431
0.9572 0.3922
0.4854 0.6555
0.8003 0.1712

Define the numerator and denominator coefficients for the rational transfer function.

Apply the transfer function along the first dimension (default) and return the 1-D digital filter of each column.

y =
0.8147 0.1419
1.0687 0.4501
0.3407 1.0058
0.9815 0.9934
0.8287 1.1582
0.2633 0.8874
0.3312 0.2132
0.6131 0.8918
1.0801 1.1123
1.1809 0.9012
0.3938 0.9380
1.0494 0.9307
1.1670 0.5784
0.7188 0.7712
0.9440 0.3254

Compare the original data and the filtered data using an overlaid plot for each column.

Plot the first column of input and filtered data.

Plot the second column of input and filtered data.

Alternatively, you can filter the rows of a matrix by specifying `dim = 2`

.

Consider the 2-by-15 matrix, `x'`

, whose rows contain the data to filter.

Filter the matrix along the second dimension. Use the default initial conditions for filter delays.

y2 =
Columns 1 through 7
0.8147 1.0687 0.3407 0.9815 0.8287 0.2633 0.3312
0.1419 0.4501 1.0058 0.9934 1.1582 0.8874 0.2132
Columns 8 through 14
0.6131 1.0801 1.1809 0.3938 1.0494 1.1670 0.7188
0.8918 1.1123 0.9012 0.9380 0.9307 0.5784 0.7712
Column 15
0.9440
0.3254

`y2`

is the transpose of `y`

from above.

Use initial and final conditions for filter delays to filter data in sections, especially if memory limitations are a consideration.

Generate a large random data sequence and split it into two segments, `x1`

and `x2`

.

The whole sequence, `x`

, is the vertical concatenation of `x1`

and `x2`

.

Define the numerator and denominator coefficients for the rational transfer function,

Filter the subsequences `x1`

and `x2`

one at a time. Output the final conditions from filtering `x1`

to store the internal status of the filter at the end of the first segment.

Use the final conditions from filtering `x1`

as initial conditions to filter the second segment, `x2`

.

`y1`

is the filtered data from `x1`

, and `y2`

s the filtered data from `x2`

. The entire filtered sequence is the vertical concatenation of `y1`

and `y2`

.

Filter the entire sequence simultaneously for comparison.