A better way to do the clip operation is to create a function
that takes two input arguments (a signal vector and a scalar
threshold) and returns an output signal vector.
We will show that most functions can be written according to a
In order to continue with the clip example, use your editor
to create an ASCII file
clip.m that contains the following
We can break down the M-file
clip.m into four elements:
functionas the very first thing in the file. The information that follows
functionon the same line is a declaration of how the function is to be called and what arguments are to be passed. The name of the function should match the name of the M-file ; if there is a conflict, it is the name of the M-file which is known to the MATLAB command environment.
Input arguments are listed inside the parentheses following the
Each input is a matrix.
The output argument (a matrix) is on the left side of the equals sign.
Multiple output arguments are also possible, e.g., notice how the
clip.m returns the number of rows and
number of columns into separate output variables.
Square brackets surround the list of output arguments.
Finally, observe that there is no explicit return of the outputs;
returns whatever value is contained in the output
matrix when the function completes.
return just leaves the function, it does
not take an argument.
clip the last line of the function assigns the clipped
The essential difference between the function M-file
and the script
is dummy variables versus permanent variables.
The following statement creates a clipped vector
wclipped from the input vector
wclipped are permanent variables in
The temporary arrays created inside
i) exist only while
clip runs; then they are deleted.
Furthermore, these variable names are local to
clip.m so the
x could also be used in the workspace as a permanent name.
These ideas should be familiar to anyone with experience using a
high-level computer language like C, FORTRAN or PASCAL.
%sign is a comment line. The first group of these in a function are used by MATLAB 's help facility to make M-file s automatically self-documenting. That is, you can now type
help clipand the comment lines from your M-file will appear on the screen as help information!! The format suggested in
clip.mfollows the convention of giving the function name, its calling sequence, definition of arguments and a brief explanation.
sizefunction. In the case of the
clipfunction, we want to restrict the function to operating on vectors, but we would like to permit either a row or a column . Therefore, one of the variables
ncolsmust be equal to one; if not we terminate the function with the bail out function
errorwhich prints a message to the command line and quits the function.
clipfunction, the actual clipping is done by a
forloop which examines each element of the
xvector for its size versus the threshold
Limit. In the case of negative numbers the clipped value must be set to
-Limit, hence the multiplication by
sign(x(n)). This assumes that
Limitis passed in as a positive number, a fact that might also be tested in the error checking phase.