Tags: Call To Action Definition EssayThesis UniversityReflective Essay HelpAn Invention That Changed The World EssayVenture Capital Business Plan TemplateProblem Solving AbilityBusiness Plan ChartsGood Comparison Essays
For example, the existence of a file on disk called STAT.M with: function [mean,stdev] = stat(x) n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).^2)/n); defines a new function called STAT that calculates the mean and standard deviation of a vector.Chapter 3: Writing MATLAB Functions 3 52 The commands and functions that comprise the new function must be put in a file whose name defines the name of the new function, with a filename extension of '.m'.
A particular caution in defining your own functions is to make sure that your names do not conflict with any of MAT- LAB s predefined functions The m-file name must be the same as the function name, e.g., my_fct is saved as my_fct.m All variables created and used within a function are local to that function, and will be destroyed when you are done using the function Likewise the only MATLAB workspace variables that the function has knowledge of are those that are passed into it via the input parameter list, i.e., in1, in2, etc., Chapter 3: Writing MATLAB Functions 3 53 Example: A common function used in signals and systems problem solving is a function for generating a rectangular pulse We would like to create a new function rect(t) that allows to input a vector (a scalar version would trivial and not as useful) of time samples t and returns the corresponding functional values rect() t function x = rect(t) % RECT x = rect(t): A function that is defined % to be 1 on [-0.5,0.5] and 0 otherwise.
% Initialize a vector with zeros in it that is % the same length as the input vetor t: x = zeros(size(t)); % Create an index vector that holds the indices % of t where abs(t) RECT x = rect(t): A function that is defined to be 1 on [-0.5,0.5] and 0 otherwise.
Problem Solving Examples Chapter 3 of the text contains a problem solving example in the area of speech signal analysis.
In this section we will first consider this particular problem.
Run a test vector into the function and plot the results» t = -2:.01:2;» x = rect(t);» plot(t,x); grid;» axis([ ])» title('the rect Function in Action',...
'fontsize',18)» ylabel('x(t)','fontsize',16)» xlabel('t','fontsize',16) The rect Function in Action x(t) t Chapter 3: Writing MATLAB Functions 3 55 Example: Practice! 108 (2) Develop and test (plot results for a test vector input) user written functions to compute the following: 2.
Ampli- Variance (standard deviation is σ) var( x) = σ 2 = ( 0.5 µ ) 2 ( 1.2 µ ) 2 ( 2 µ ) 2 ( 0.8 µ ) = Average power average power Average magnitude average magnitude ( 2) = = Zero crossings count (by inspection) = = zero crossings = 2 MATLAB Solution: To formulate the MATLAB solution we for the most part can use standard functions we are already familiar with. How do we import a wav file into the MATLAB work space, 2.
Develop an algorithm to count the zero crossings To import a wav file we will use one of the MATLAB sound functions Chapter 3: Problem Solving Examples 3 48 tude values are in the range [-1, 1].
Next we will briefly investigate the MATLAB signal processing toolbox, which is an extension to the MATLAB core.
The signal processing toolbox is also included in the student edition. magnitude signal zero crossings signal plot Sample/Hand Calculation: Since the wav file we must process contains thousands of samples, a sample calculation will be performed using a short made-up record of data: Mean xn [ ] = ( 2) mean( x) = µ = = Chapter 3: Problem Solving Examples 3 47 » help wavread WAVREAD Read Microsoft WAVE (".wav") sound file.