Lab 02: An Experiment With Tuning Fork Synthesis of Sinusoidal
Lab 02: An Experiment With Tuning Fork Synthesis of Sinusoidal
Pre-Lab: Read the lab handout and section 2.7 in the textbook on tuning forks. Answer the following
questions:
1. This lab will use tuning forks with frequencies ranging from 128 Hz to 4096 Hz. Assuming that the
stiffness of the tuning forks is the same, comment on the mass of the tuning forks as the frequency
increases. Verify your hypothesis when you get to the lab.
1 Overview
In this lab, we will use a microphone and a computer equipped with an A to D converter to digitize a
sinusoidal signal from different tuning forks. The microphone will convert the sound signal generated by
the tuning fork to an electrical signal, which in turn is converted to a sequence of numbers stored in the
digital computer. The computer samples the output of the microphone at specified sampling rate, Fs. The
resulting signal can be displayed on computer screen as a sinusoidal curve. Characteristics of the sound
wave, such as its period T and frequency can be determined from this curve. Knowing the wave's period,
its frequency f is easily computed using the formula
f = 1 /T 2.1
Figure 1: Schematic diagram of A-440 tuning forking. A-440 is a reference frequency at which the tuning
emits a single hum at this frequency.
1
2 Matlab Preliminaries
In this section, you will write an M-File function that takes input sound signal at specific sampling rate and
stores it as an output file in wav format. The input signal will be the tuning fork signal when you strike it
and expose to vibration.
2.1 Introduction to Using Functions in Matlab
Functions are M-files that can accept input arguments and return output arguments. The names of the M-
file and of the function should be the same. Functions operate on variables within their own workspace,
separate from the workspace you access at the MATLAB command prompt.
The first line of a function M-file starts with the keyword function. It gives the function name and order of
arguments. A simple function, called compmag5 that computes the five times the magnitude of a complex
number of the form, x+jy is given below. In this case, there are two input arguments and one output
argument.
a=5
% compute magnitude
z =a* sqrt(x^2+y^2);
The next several lines, up to the first blank or executable line, are comment lines that provide the help
text. These lines are printed when you type
>>help compmag5
The first line of the help text is the H1 line, which MATLAB displays when you use the lookfor command
or request help on a directory. The rest of the file is the executable MATLAB code defining the function.
The variable a introduced in the body of the function, as well as the variables on the first line, m, x and y,
are all local to the function; they are separate from any variables in the MATLAB workspace. This means
that if you change the values of any variables with the same name while you are in the function, it does
not change the value of the variable in your workspace.
If no output argument is supplied, the result is stored in ans. You can terminate any function with an end
statement but, in most cases, this is optional. Functions normally return when the end of the function is
reached.
The function is used by calling the function within Matlab or from a script. For example, the commands
below can all be used to call compmag5 and get a result of m=10.
m=compmag5(2,2);
x=2;y=2; m=compmag5(x,y);
2
2.2 Audio Input using a Matlab function
Matlab inputs the signal from a microphone using either the wavrecord or audiorecord commands. The
function file, auinput, below automates the process of collecting, saving, and plotting audio data using a
microphone. You should use this function to gather data for parts 3 and 4 of this lab. This function
introduces several new MATLAB functions which are defined below:
Ceil: B = ceil(A) rounds the elements of A to the nearest integers greater than or equal to A. For complex
A, the imaginary and real parts are rounded independently. Example, if a = [-1.9, -0.2, 3.4], b =[-1, 0, 4].
wavrecord: Records sound using a PC-based audio input device. y = wavrecord(n,Fs) records n samples
of an audio signal, sampled at a rate of Fs Hz (samples per second). The default value for Fs is 11025
Hz. Standard sampling rates for PC-based audio hardware are 8000, 11025, 2250, and 44100 samples
per second.
wavwrite: wavwrite(y,Fs,'filename') writes the data stored in the variable y to a Microsoft WAVE file called
filename. The data has a sample rate of Fs Hz and is assumed to be 16-bit. Amplitude values outside the
range [-1,+1] are clipped prior to writing.
wavplay(y,Fs) plays the audio signal stored in the vector y on a PC-based audio output device. You
specify the audio signal sampling rate with the integer Fs in samples per second. The default value for Fs
is 11025 Hz (samples per second).
function y=auinput(Fs,total_t,fn);
% this function collects mono audio signals for total_t seconds
% at a sampling rate of Fs hertz. The result is saved in a wav file named
% by the string, fn
% the file is saved as a wave file and played back.
% the resulting signal is plotted
%
% Usage Example (gives an example of how to use this function)
% Fs=8000;total_t =5;fn='test';
% y=auinput(Fs,total_t,fn);
% written by J.A. Dickerson, 2004
3
% save signal in a file
wavwrite(y,Fs,fn);
% Play the signal back on the speakers
wavplay(y,Fs);
4 Lab Procedure
Signal Collection
1. Collect tuning fork signals and save wavfiles using the function auinput. Use a
microphone and collect signals for two different tuning forks at frequencies at or under
2048 Hz using a sampling period of .125 ms. Make sure that you give each file a
different name to avoid overwriting.
2. Measure the frequency of these signals using a plot of the signal. Include the plots
that you used in your lab report. Be sure to label your graph and all the time units using
the methods given in section 2.3 above. Show how you measured the signal. In order to
have the proper time index on the plot, the sampling interval (the space between
samples) used in this signal must be calculated (Ts=1/Fs). The time vector must then be
scaled to give the proper time values:
4
tscale = (1:length(y))*Ts;
plot(tscale,y);
3. Compare the measured frequency with that on the tuning fork. What are possible
sources of error?
4. Repeat steps 1 and 2 using a sampling frequency of 44,100. Are your estimates of
frequency more accurate? Why or why not? Give plots.
Frequency Analysis
5. Compute the magnitude spectrum of the signals in step 1. Verify that the peaks occur
at the frequency of the tuning fork. Turn in your plots. (Note: If your signal is not close,
then your time window for collection is probably too long.) What is the amplitude in
decibels of the signal in each case? Compare the tuning fork frequency estimated by
the frequency spectrum with the one estimated using the time signal.
6. Comment on the other signals present in the spectrum. Why don’t you see a nice
clean delta function like in the notes?
5. Lab Report
1. Submit the verification sheet to your TA when you have finished the lab.
2. Answer all questions in section 4 of this lab and include all requested plots and
calculations with clearly labeled graphs.
3. Include all your MATLAB code in an appendix of your report.
5
Lab 2
Instructor Verification Sheet
Staple this page to the end of your Lab Report.
Name:---------------------------------------------------- Date:-------------------------------
1. Check that the pre-lab was performed and the question was answered
2. Capture tones from two tuning forks using wavrecord. Play the captured signals for the TA.
_____________________________________________
3. Plot the signal using a time axis based on the sampling frequency of the signal. Time values
verified_____________
4. Label the axes of the plot in (3) using the xlabel and ylabel commands. Axes labeled_______
5. Determine the period and the frequency (in hertz) of each of the tuning fork signals using a plot
showing 3-4 cycles of the signal. Verify that this signal is close to the frequency on the tuning
fork. Frequency listed on tuning fork? ____________; Frequency found from the
plot?__________
5. Plot the frequency response of the signals. Show the labeled plots to the Lab Assistant.