aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2012-06-21 10:44:31 -0500
committerJoshua Peek <josh@joshpeek.com>2012-06-21 10:44:31 -0500
commit540f2a0941cdfa9032731f7b02aa595c8a8bd8b3 (patch)
tree391fd22955ee31389bfdee10771a8b31b2ba4e7d /test
parent497da862629490fb25c299f7a12e7129465bfafd (diff)
More matlab samples
Diffstat (limited to 'test')
-rw-r--r--test/fixtures/matlab/average.m9
-rw-r--r--test/fixtures/matlab/make_filter.m38
2 files changed, 47 insertions, 0 deletions
diff --git a/test/fixtures/matlab/average.m b/test/fixtures/matlab/average.m
new file mode 100644
index 0000000..65eef8b
--- /dev/null
+++ b/test/fixtures/matlab/average.m
@@ -0,0 +1,9 @@
+function y = average(x)
+% AVERAGE Mean of vector elements.
+% AVERAGE(X), where X is a vector, is the mean of vector
+% elements. Nonvector input results in an error.
+[m,n] = size(x);
+if (~((m == 1) | (n == 1)) | (m == 1 & n == 1))
+ error('Input must be a vector')
+end
+y = sum(x)/length(x);
diff --git a/test/fixtures/matlab/make_filter.m b/test/fixtures/matlab/make_filter.m
new file mode 100644
index 0000000..3ad6c5c
--- /dev/null
+++ b/test/fixtures/matlab/make_filter.m
@@ -0,0 +1,38 @@
+function [filtfcn, statefcn] = makeFilter(b, a)
+% FILTFCN = MAKEFILTER(B, A) creates an IIR filtering
+% function and returns it in the form of a function handle,
+% FILTFCN. Each time you call FILTFCN with a new filter
+% input value, it computes the corresponding new filter
+% output value, updating its internal state vector at the
+% same time.
+%
+% [FILTFCN, STATEFCN] = MAKEFILTER(B, A) also returns a
+% function (in the form of a function handle, STATEFCN)
+% that can return the filter's internal state. The internal
+% state vector is in the form of a transposed direct form
+% II delay line.
+
+% Initialize state vector. To keep this example a bit
+% simpler, assume that a and b have the same length.
+% Also assume that a(1) is 1.
+
+v = zeros(size(a));
+
+filtfcn = @iirFilter;
+statefcn = @getState;
+
+ function yn = iirFilter(xn)
+ % Update the state vector
+ v(1) = v(2) + b(1) * xn;
+ v(2:end-1) = v(3:end) + b(2:end-1) * xn - ...
+ a(2:end-1) * v(1);
+ v(end) = b(end) * xn - a(end) * v(1);
+
+ % Output is the first element of the state vector.
+ yn = v(1);
+ end
+
+ function vOut = getState
+ vOut = v;
+ end
+end