1 |
function RMS= rms(varargin) |
2 |
% |
3 |
% Written by Phillip M. Feldman March 31, 2006 |
4 |
% |
5 |
% rms computes the root-mean-square (RMS) of values supplied as a |
6 |
% vector, matrix, or list of discrete values (scalars). If the input is |
7 |
% a matrix, rms returns a row vector containing the RMS of each column. |
8 |
|
9 |
% David Feldman proposed the following simpler function definition: |
10 |
% |
11 |
% RMS = sqrt(mean([varargin{:}].^2)) |
12 |
% |
13 |
% With this definition, the function accepts ([1,2],[3,4]) as input, |
14 |
% producing 2.7386 (this is the same result that one would get with |
15 |
% input of (1,2,3,4). I'm not sure how the function should behave for |
16 |
% input of ([1,2],[3,4]). Probably it should produce the vector |
17 |
% [rms(1,3) rms(2,4)]. For the moment, however, my code simply produces |
18 |
% an error message when the input is a list that contains one or more |
19 |
% non-scalars. |
20 |
|
21 |
if (nargin == 0) |
22 |
error('Missing input.'); |
23 |
end |
24 |
|
25 |
|
26 |
% Section 1: Restructure input to create x vector. |
27 |
|
28 |
if (nargin == 1) |
29 |
x= varargin{1}; |
30 |
|
31 |
else |
32 |
|
33 |
for i= 1 : size(varargin,2) |
34 |
if (prod(size(varargin{i})) ~= 1) |
35 |
error(['When input is provided as a list, ' ... |
36 |
'list elements must be scalar.']); |
37 |
end |
38 |
|
39 |
x(i)= varargin{i}; |
40 |
end |
41 |
end |
42 |
|
43 |
|
44 |
% Section 2: Compute RMS value of x. |
45 |
|
46 |
RMS= sqrt (mean (x .^2) ); |