E5 Matlab 3

and electronic control of physical devices

In lecture this week, you learned how Matlab functions are defined. A function is a program that takes a list of variables, in order, as input arguments and produces an output that can be used in a Matlab expression. Each Matlab function must be in a separate file, and contain the syntax discussed in lecture.

1) Write a Matlab function called devsq.m that takes a single argument and produces a single output variable. The function should take the deviation of each element in the argument variable x (scalar, vector, or matrix) from the mean and return an output of the same size as x that contains the squares of the deviations from the mean. Post this on your E5 website as devsq.m (be sure to include several descriptive comment lines in the code following the first line and test to see if the "help" function works for your function).

Often functions are called within other functions, or within Matlab programs, to process a data set. Download the Matlab file pop.mat and put it in your Matlab default directory. It is the sound of a balloon pop, recorded at a sampling frequency of 44100 samples per second, in an echoey space to determine the reverberation time of the room. You can listen to the sound by invoking the function soundsc:

>> soundsc(pop,44100)

where the first argument is the variable array containing the data (microphone voltage samples), and the second is the playback rate in Hz. Try adjusting the playback rate to see what happens.

2) Plot the balloon pop sound using the plot command, with elapsed time in seconds on the horizontal axis and microphone voltage on the vertical axis. Use the subplot command to place this graph in the upper half of a plot window; in the lower one, you will plot the rms average of the sound, as in 3) below.

3) Using a call to the devsq.m routine from 1) above, write another function called rms.m that takes the squares of the deviations from the mean of every 100 points of the pop and averages them. Finally, this program should take the square root of the averages and thus return an array with the root-mean-squared values of the balloon pop voltages. Plot this rms function in the lower panel of the plot in 2) above.

Also in lecture, we discussed the "eval" function that can be used to include string user input into Matlab commands. This function is extremely useful for file input-output using the following patterns:

filename = input('Enter name of file in which data will be saved: ', 's'); % type "help input" for the syntax
buildstring = ['fid = fopen(''', filename, ''', ''w'');'] ; % build up a string that has the command you want
eval(buildstring); % do the command in buildstring

Note the use of double single-quotes (which become single quotes inside a literal string bounded by single quotes) to provide the required syntax for the fopen command (i.e., for a user-entered filename hello.dat the buildstring variable is an array of characters: fid=fopen('hello.dat','w'); that gets evaluated by the eval command).

4)Write a program (not a function) that prompts the user for a filename and some data, opens the file, and writes some data into the file using the fprintf command. Don't forget to fclose the file before your program finishes. Verify that your program works by opening the user's file in the Matlab editor.

Finally, we discussed some basic concepts in electronics and how solenoids are activated. Refer to the circuit diagram in lecture (and reproduced on the whiteboard) and build a solenoid activation circuit on the breadboards provided. Test the circuit with the Midi keyboard and controller module by playing a "middle C" to deploy the solenoid.


Comments or Questions?
 Send me email         

Carr Everbach
Engineering Department
Swarthmore College