Top Banner
Fun with MATLAB A Motivation to Image and Audio Processing using MATLAB.
31

Fun with MATLAB

Jun 25, 2015

Download

Engineering

ritece

This presentation shares some basic concepts about audio processing and image processing using MATLAB and was used as teaching material for an introductory workshop.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Fun with MATLAB

Fun with MATLAB

• A Motivation to Image and Audio Processing using MATLAB.

Page 2: Fun with MATLAB

Images

• An image is a collections of various pixels with specific color information.

• Pixels are the building blocks of an image. In other words, a pixel is the smallest possible image that can be depicted on the screen.

• In the general case we say that an image is of size m-by-n if it is composed of m pixels in the vertical direction and n pixels in the horizontal direction.

Page 3: Fun with MATLAB

Applications Of Image Processing

• Image processing has an enormous range of applications; almost every area of science and technology can make use of image processing methods. Here is a short list just to give some indication of the range of image processing applications.

• 1. Medicine

Page 4: Fun with MATLAB

• Inspection and interpretation of images obtained from X-rays, MRI or CA T scans,

• analysis of cell images. etc…2. Industry• Automatic inspection of items etc…3. Law enforcement• Fingerprint analysis,• sharpening or de-blurring of speed-camera

images. etc…

Page 5: Fun with MATLAB

Image Types

1. Binary Image – each pixel is either white(1) or black (0).

2. Greyscale Image – each pixel is a shade of grey, from black (0) to white (255).

3. True Color or RGB – here each pixel is defined by the amount of red, green and blue component in it.

4. Indexed Image – Here information for each pixel is basically index in an associated colormap .

Page 6: Fun with MATLAB

Importing images

• imread function• >>image=imread(‘cameraman.tif’);

• This function reads the default image ‘cameraman.tif’ with all the pixel data stored in the variable image.

• Try displaying the values stored in image. >>image %that’s it.

Page 7: Fun with MATLAB

Displaying images

• imshow function• We can display the image we recently read

using this function…

>> imshow(image);

Page 8: Fun with MATLAB

Rotating the images

• im_new =imrotate ( image matrix ,angles in degrees);

• imrotate function• This function rotates the image anticlockwise by

the degree measure you specify.• Try with negative angles.

Page 9: Fun with MATLAB

Audio Processing

Page 10: Fun with MATLAB

Basic Signal Characteristics

Amplitude

Frequency

Phase

Page 11: Fun with MATLAB

Digital Music

• It is a discrete time information stored by sampling a continuous time signal at a particular rate, called sampling frequency.

• Discrete??? Continuous??? Sampling???

Page 12: Fun with MATLAB

A Continuous time signal is one which is defined for each and every value of time.

Page 13: Fun with MATLAB

A discrete time signal is rather specified at particular intervals of time.

Page 14: Fun with MATLAB

Sampling

• This is the process of breaking up a continuous time waveform by specifying it only at discrete points in time.

• And this is the process by which all digital data e.g. music, video files etc. are being stored and manipulated today.

Page 15: Fun with MATLAB

An example of a sinusoidLet’s create a basic sinusoidal signal.

Fs=44100; %sampling rateT=1/Fs; %sampling periodt=[0:T:0.25]; %create a time vectorf1=50; %frequency in Hertzomega=2*pi*f1; %angular frequency in radiansx1=sin(omega*t); %sinusoidal signal with amplitude=1

plot(t,x1);xlabel(‘Time(seconds)’);ylabel(‘x(1)’);title(‘simple sinusoid’);

sound(30*x1); %it will play the sinusoid signal which you have created !

Page 16: Fun with MATLAB

Sound

• The previous code played a sinusoidal signal !• What’s happening : Sound is recorded in discrete form and then digitized.

These values are then stored and played later.

MatLab can be used to read a sound file and then play it. As the song is nothing but a collection of values in a matrix.

Page 17: Fun with MATLAB

• sound(y , Fs) is one of the many commands used to play the values stored in matrix ‘y’ at rate of Fs.

• For e.g. You may load an already present sound file as:

>> load gong.mat % this code loads the audio file gong.mat with sampled values

% stored in a default matrix ‘y’ and default playing rate at ‘Fs’ % now type the following line

>> sound(y , Fs) %the bang!!! You may enter different values of Fs and listen the

sound.

Page 18: Fun with MATLAB

• You may try loading and playing these files too:

• chirp.mat • handel.mat • splat.mat • laughter.mat • train.mat

Page 19: Fun with MATLAB

Reading Music Files in MatLab

• [y , Fs]= wavread (‘Complete File Address’); reads the .wav file whose address has been specified.

Here ‘y’ contains the sampled values of the audio data and ‘Fs’ contains the original sampling frequency.

• You may print these two values as: >>y %this may be very large and consume a lot of %time in displaying the matrix y. Abort : Ctrl + C.

>>Fs

Page 20: Fun with MATLAB

The Music matrix

• Monophonic Music : it is represented by a NX1 matrix.

The N rows contain N samples of the sound (music) to be played. Simple…

• Stereo Music : it is represented by an NX2 matrix. What’s this ??

Page 21: Fun with MATLAB

How music is recorded?

Page 22: Fun with MATLAB

Differences between Mono And Stereo

• In Mono form, same information is played in both the ears. But in Stereo form, the information played in left and right ears are independent.

• That’s why Stereo music is a NX2 matrix. First column contains samples for Left channel and second column for the right channel.

Page 23: Fun with MATLAB

Let’s Experiment

• Import the .wav music file in MATLAB as:

>>[y , Fs]= wavread ( ‘full path of the .wav file’); If you see in the workspace there are two variables y and Fs. If y

is an NX2 matrix then the music you imported is … yes, Stereo.

• We have the sound matrix. Let’s extract the left channel music and right channel music separately.

• How would you do this??? Do you remember the matrix

manipulation basics???

Page 24: Fun with MATLAB

Separating the Stereo Channels

• >> left = y(:,1); %left is a NX1 matrix with left channel samples.• Similarly, >>right = y(:,2);• Wow… Let’s listen the differences.• Listen the left channel ---- >> sound(left , Fs);• Listen the right channel--- >> sound(right , Fs);• Listen the stereo – exactly, you got it right… >>sound( y, Fs);

Page 25: Fun with MATLAB

• In most popular music recordings, the vocal track is the same on the left and right channels (or very similar). The volume of the various instruments are more unevenly distributed between the two channels. Since the voice is the same on both channels, what would happen if we subtract one channel from the other and listen to the result?

• sound(left , Fs); % Original left channel • sound(left-right , Fs); % virtually no vocal • The voice is virtually eliminated !!! Crazy stuff…

Page 26: Fun with MATLAB

Saving sound File as a .wav Music File

• wavwrite() function is used to write the sound matrix you created, as a .wav format music on to your disk permanently.

Sounds cool…• What’s the complete format >>wavwrite(sound matrix (for e.g. y) , Fs

(sampling frequency), ’ file_name ’);

Page 27: Fun with MATLAB

Demo with a HotchPotch Song

• Probably you would be listening to a crazy song like this for first time.

• Import the .wav sound file, hotchpotch (provided to you) to the workspace. What do you write for that ???

Exactly, [y , fs ]= wavread(‘ complete address of

hotchpotch.wav on your system’);

Page 28: Fun with MATLAB

• Give one of the earplugs to your neighbor .

• Now, play the song. • Ofcourse , >>sound(y , fs);• Ask your friend – what song did s/he listen??• Is s/he lying???

Page 29: Fun with MATLAB

What do you think ???• That’s some crazy mixing… Who listens to songs

like such???• Let’s do some manipulation…• Quickly extract the left and right audio channel

data .• left=y(:,1);• right=y(:,2); • Now listen to each channel separately : • sound(left , fs); and then sound(right ,fs );

Page 30: Fun with MATLAB

We think all of it was pretty interesting.

• But, What about you???

• Please, write down your reviews???

Page 31: Fun with MATLAB

Keep Learning. Have Fun.

Thank you…