SPHSC 53 Speech Signal Processing UW Summer 6 Handout - Introduction to plots in Matlab 7 Signal analysis is an important part of signal processing. And signal analysis is not complete without signal visualization. Matlab has a large number of data visualization tools, which we will introduce and explore in this handout. Before going into the plotting functions of Matlab, it may be helpful to get a quick reminder of some basic Matlab concepts. A great way to do this is through an instructional video that is provided with Matlab 7. This video can be accessed in Matlab with the command: >> playbackdemo( desktop ) or by going to Matlab s Start menu (not to be confused with Windows Start menu), and selecting Demos Desktop Tools and Development Environment Desktop and Command Window. Interactive plot tools New in Matlab 7 is an extensive set of interactive plot tools. With these tools it is possible to create most plots without using m-commands directly. The best introduction to these tools is given in an instructional video that is provided with Matlab 7. You can access this video in Matlab with the command: >> playbackdemo( PlotTools_viewlet_swf ) or by going to Matlab s Start menu and selecting Demos Graphics Interactive plot creation. Creating plots with m-commands The interactive plot tools include the option to generate m-commands for the interactively created plots. Those m-commands can be saved in a file that can be used later to recreate the plots. At that time, similar plots can be created using different data. M-commands are often used by experienced Matlab users to create figures, because they provide a quick, easy and reproducible way of generating figures. For this part of the course, the following m-commands are the most important for creating figures. stem This command creates a discrete sequence or stem plot. stem(y) plots the data sequence y as stems from the x-axis, terminated with circles for the data value. stem(x,y) plots the data sequence y at the values specified in x. Examples:.5 >> y = [ ]; >> stem(y);.5 3 4 5 6
SPHSC 53 Speech Signal Processing UW Summer 6.5 >> x = -:3; >> y = [ ]; >> stem(x,y);.5 - - 3 The stem command allows optional parameters to specify the color and style of the stem lines and stem markers, see help stem for details. plot This command produces a linear plot. plot(y) plots the vector y against its index. plot(x,y) plots vector y versus vector x. The line type, plot symbol and color may be specified in a third optional parameter, and multiple linear plots may be combined in a single command, such as plot(x,y,x,z). More information about this can be found by typing help plot in the Matlab command window. An advanced use of the plot command is shown in the example below. - >> x = -:; >> y = [ - -]; >> x = -:6; >> y = cos(*pi*x/7); >> plot(x,y, :ob,x,y, -xr ); - - 4 6 hold The hold command sets, clears or toggles the hold state of the current axes. When an axes is created (by using a plot or stem command for example), its hold state is set to off. Subsequent plot or stem commands will erase previous plots in the axes before drawing new plots. The hold on command can be used to turn the hold state to on, so that plots can be combined. hold off turns the hold state off, and hold by itself toggles the hold state of the current axes. For example, instead of the command >> plot(x,y,, :ob,x,y, -xr ); in the example above, we could have used the commands >> plot(x,y, :ob ); >> hold on >> plot(x,y, -xr ); to get the same effect.
SPHSC 53 Speech Signal Processing UW Summer 6 xlabel, ylabel, title These commands respectively add text beside the x-axis, y-axis or above the current axes. Their use is illustrated in the example below. Amplitude.5.5 -.5 Two signals (continued) >> xlabel( Index (n) ); >> ylabel( Amplitude ); >> title( Two signals ); - -.5 - - 4 6 Index (n) legend The legend displays a legend for the current axes. legend(string, string, string3,...) puts a legend on the current plot using the specified strings as labels. The example below shows how to add a legend to the plot from the previous example. The labels must be specified in the order that the plots were made..5 Two signals Line Sinus (continued) >> legend( Line, Sinus );.5 Amplitude -.5 - -.5 - - 4 6 Index (n) subplot The subplot command creates axes in tiled positions. subplot(r,c,i) breaks the figure window up in r by c axes, and selects the i-th axes for the current plot. The axes are numbered left to right, top to bottom. For example, to create a by 3 matrix of axes, and access each of them in turn, you would need the following commands: >> subplot(,3,) >> subplot(,3,) >> subplot(,3,3) >> subplot(,3,4) >> subplot(,3,5) >> subplot(,3,6) 3
SPHSC 53 Speech Signal Processing UW Summer 6 In the example below, the subplot command has been used to plot the two signals from the previous example in separate axes. subplot(,,) (continued) - - - - subplot(,,) >> subplot(,,); >> plot(x,y, :ob ); >> subplot(,,); >> plot(x,y, -xr );.5 -.5 - - 3 4 5 6 figure, clf, cla, close The figure, clf, cla and close commands manipulate figures and axes. The figure command, without any parameters, creates a new figure window and makes it the current window for new plots. figure(n) creates figure window n if it didn t exist and makes it the current window for new plots. The clf command clears the current figure, erasing all axes in it. To clear a specific figure, use a combination of figure(n), clf. The cla command clears only the current axes, erasing all plots, legends, etc. in the axes. To clear a specific axes, use a combination of figure(n), subplot(r,c,i), cla. The close command closes the current figure, and close(n) closes figure n. A note on current figures and axes: in Matlab, the current figure is the figure window that was selected last, either by a mouse click anywhere in the window, or by selecting it with the figure command. Similarly, the current axes is the axes that was selected last, either by a mouse click anywhere in the axes, or by selecting it with the subplot command. In-class exercises Exercise. Basic signals: impulses [from McClellan, ex.., page 3] The simplest signal is the (shifted) impulse sequence n= n δ[ n n ] = n n To create an impulse in Matlab, we must decide how much of the signal is of interest. For reasons we will see later, we are often interested in a number of points starting at n=. For example, we might want to see 3 points starting at n=, in which case the following Matlab code will create an impulse imp: >> n = :9; >> imp = zeros(size(n)); >> imp() = ; 4
SPHSC 53 Speech Signal Processing UW Summer 6 Notice in the code above that the n= index must be referred to as imp(), due to Matlab s indexing scheme which starts at. Instead of the third command above, a more advanced way of setting the point n= to is >> imp(n==) = ; This uses a Matlab technique called logical indexing to select which element of the imp vector to set to. In essence, this statement says: wherever the vector n equals, put a at the same location in the imp vector. Vectors n and imp must be of the same length for this to work. Given this background information and the introduction to the (interactive) plot tools of Matlab, generate and plot the following sequences. In each case, the horizontal (n) axis should extend only over the range indicated and should be labeled accordingly. Each sequence should be displayed as a discrete-time signal using stem. a. x [ n ] =.9 δ [ n 5], for n b. x [ n ] =.8 δ [ n ], for -5 n 5 c. x 3 [ n ] =.5 δ [ n 333], for 3 n 35 d. x 4 [ n ] = 4.5 δ [ n+ 7], for n Exercise. Basic signals: sinusoids [from McClellan, ex.., page 4] Another very basic signal is the (co)sine wave. In general, it takes three parameters to describe a real sinusoidal signal completely: amplitude (A), frequency ( ω ) and phase (φ ): xn [ ] = Acos( ω n+ φ) Generate and plot each of the following sequences. In each case, the horizontal (n) axis should extend only over the range indicated and should be labeled accordingly. Each sequence should be displayed as a sequence using stem. x[ n] = sin n, for n 5 π a. 7 x [ n] = sin n, for 5 n 5 π b. 7 c. x 3 [ n ] = sin(3 πn+ π / ), for n π x [ n] = cos( n), for n 5 d. 4 3 Explain why x [ ] 4 n is not a periodic sequence. Note: the stem command is most useful for short sequences, and in cases where you want to stress the discrete nature of the signal you re working with. In most practical cases, however, it is more convenient to use the plot command to display sequences. But beware that such a representation creates the illusion that you re dealing with a continuous signal, where in fact you re not. For example, if you use plot to display x 4 [ n ] in Exercise., it becomes almost impossible to tell that the sequence is not periodic. Exercise.3 Loading and analyzing a speech signal Get the file ex_3.wav from the class website, and save it in a local folder (suggested location C:\Temp\SPHSC53\ex_3.wav). The file contains the spoken word zero sampled at khz. Change the current directory in Matlab to the directory that contains the saved file. 5
SPHSC 53 Speech Signal Processing UW Summer 6 a. Load the ex_3.wav file into Matlab. You can either use Matlab s Import Wizard, by double-clicking on the filename in the current directory window, or use the wavread command (see help wavread for details). b. Plot the speech signal against its index by using the interactive tools or by using the plot command. c. Plot the speech signal as a function of time. Hint: you need to create a time vector t, and then plot the signal with plot(t,y). d. Determine the duration of the signal in seconds. It may be helpful to use the figure s zoom, pan and data cursor tools. Voiced speech, such as vowels, is characterized by a series of high-energy peaks in the speech signal. Those peaks are created by the repeated opening and closing of the vocal chords. e. Make a rough estimate of the distance in seconds between the peaks in the zero speech signal around t=. and t=.4 seconds, corresponding to the two vowels. Again, it may be helpful to use the figure s zoom, pan and data cursor tools. f. Convert the measured distances in seconds from part e into an estimate of the fundamental frequency (in Hz) of the speech signal around t=. and t=.4 seconds. 6