MATLAB is a useful software tool for engineering and computational mathematics. It also plays a very important role in scientific and engineering plots. This project deals with 2D and 3D graphics plotting in MATLAB. It also explains the audio signal manipulations using plotting in MATLAB.

Familiarisation with basic MATLAB objects like vectors and matrices will help in understanding the subject better before delving into details. MATLAB has an excellent set of graphic tools. Plotting the given data set or the results of computation is possible using very few commands.

** Plotting in MATLAB**

2D plotting. To use the ‘plot’ function in MATLAB, you should first make sure that the matrices/vectors you are trying to use are of equal dimensions. Here we have taken a simple plotting example using the plot ( ) function as given below (its output plot is shown in Fig. 1):

t = [1 2 3];

plot (t,x);

You can also label the axes of the plot in Fig. 1 by using commands as given below. The plot becomes a time-distance plot as shown in Fig. 2.

for x axis

ylabel(‘Distance(m)’); %

label for y axis

title(‘Plot of Distance over

Time’); % giving the title

legend(‘Trial 1’);

legend(‘Location’,

’Northwest’); % move legend

to desired location

Now, take an example of plotting a sine function using the following commands (its output is shown in Fig. 3):

y = sin(x);

plot(x, y);

Now, if you want to change the colour of line or pattern, use the command:

Here ‘r’ is the colour of the line. The ‘:’ (colon) is used for the line pattern and ‘4’ is the line width. Its output is as shown in Fig. 4. If you want to know more about the colours and patterns of line in MATLAB, type ‘help plot’ in command window and you will see the entire list.

To plot another graph with green colour on the same screen, we use ‘hold’ command as given below (its output is shown in Fig. 5):

y = sin(x);

z = cos(x);

plot(x, y, ‘r’);

hold on

plot(x, z, ‘g’);

The subplot ( ) function divides the current figure into rectangular panes that are numbered row-wise. An example for ‘subplot’ command is given below (its output is shown in Fig. 6):

x = 0: pi/100: pi*2;

y = sin(x);

z = cos(x);

t = exp(-x);

u = (x.^3);

subplot(2,2,1);

plot(x,y);

subplot(2,2,2);

plot(x,z);

subplot(2,2,3);

plot(x,t);

subplot(2,2,4);

plot(x,u);

An example of the polar command is given below (its output is shown in Fig. 7):

x = 0:0.2:5*pi;

y = x.^2;

polar(x, y,’*’);

Similarly, we also have other commands for 2D graphics like bar, stairs, loglog and semilogy.

* 3D Plotting. *There are different ways to plot in multiple dimensions in MATLAB. A type of 3D plot that may be useful is a surface plot, which requires you to generate some kind of x-y plane and then apply a third function as the z dimension. The example for a surface plot using surf ( ) function is given below (its output is shown in Fig. 8):

set up 2-D plane

z = x.*exp(-x.^2-y.^2); % plot

3rd dimension on plane

surf(x,y,z) ; % surface plot

Similarly, we also have other commands for 3D graphics like plot3, contour and mesh.

**Audio signal manipulation using MATLAB**

MATLAB is a widely used environment for signal processing and analysis. Here, we introduce some key MATLAB concepts and functions that are useful to create and manipulate audio signals.

Let us use one audio sample ‘.wav’ file and do some manipulation on it by using some commands and plot a graph of the manipulated signal.

You can download the .wav file from the Internet or create it yourself using a suitable sound recording application. Once that is done, place the .wav file in the MATLAB folder.

You can read, play and store the audio file in MATLAB. To read the file use ‘wavread’ command. The various syntaxes used are given below:

y = wavread (filename), loads a wave file specified by the string filename, returning the sampled data in y.

[y, Fs] = wavread (filename), returns the sample rate (Fs) in Hertz used to encode the data in the file.

[y, Fs, nbits] = wavread (filename), returns the number of bits per sample (nbits).

For example, to read and play the handel.wav audio file, the following commands are used (its output is shown in Fig. 9):

wav’); % read the audio

file.

sound(a); % to play an

audio

b = flipud(a); % to

reverse the audio

sample

sound(b); % to play

an audio

Here, ‘a’ stands for the discrete signals. To see the number of samples in variable ‘a’, we write length command ‘length(a)’ in the command window as shown in Fig. 9.

Considering that the handel.wav file has a sample rate of 44,100, which means that one second of audio has 44,100 samples, we divide the total number of samples by 44,100 and get the duration of audio as 1.6579 seconds.

To plot the signal, type the command given below (its output is shown in Fig. 10):

X-axis denotes the number of samples (0 to 80,000), and Y-axis denotes the amplitude.

Now, let us see how to manipulate the audio signal.

a1 = a; % stores the value into new variable (a1) so that we can manipulate the previous audio signal stored in variable (a). Let us make the amplitude zero of samples from 20,000 to 40,000 by writing the syntax as given below (its output is shown in Fig. 11):

plot(a);

Now, play the original audio signal which is stored in variable ‘a1’ and play the modified audio signal in ‘a’ using following commands:

sound(a);

While listening to signal ‘a,’ you will find that the sound will stop in between and then start again. This shows that the samples were zero in between.

Next, we plot both the signals, the original audio signal and modified signal, to see the difference in them using the following commands (its output as shown in Fig. 12).

hold on

plot(a, ‘r’);

Here, ‘red’ represents the modified signal and ‘blue’ the original signal.

* Voice recording, playback and plotting.* To record your voice, use the following commands:

**audio = audiorecorder(Fs, nBits,**

** nChannels);**

Here, Fs is the sampling rate in Hz. Valid values for Fs depend on the specific audio hardware installed. Typical values supported by most sound cards are 8000, 11,025, 22,050 and 44,100Hz—default value is 8000Hz.

Nbits is bits per sample. Valid values for nBits depend on the operating system. For Windows, it is 8, 16, 24—default is 8.

nChannels is the number of channels: 1 (mono) or 2 (stereo)—default is 1.

For example,

record(audio, 5); % 5 seconds for

recording.

test = getaudiodata(audio); % Store

data in double-precision array.

sound(test);

plot(test);

As shown in Fig. 13, the plot is of recorded audio signal at the sampling rate of 8000Hz. Now, we will change the sampling rate to 80Hz, record and plot audio signal as shown in Fig. 14.

record(audio2, 5); % 5 seconds for

recording.

test2 = getaudiodata(audio2); % Store

data in double-precision array.

sound(test2);

plot(test2);

Next, we plot both the audio signals of different sampling rates (8000Hz and 80Hz) on the same screen. The commands for the same are given below:

hold on

plot (test 2, ‘r’);

The output plot is shown in Fig. 15.

*The author is a B.Tech in electronics and communication from SRCEM, Gwalior*