Fun with MATLAB

Preview:

DESCRIPTION

This presentation shares some basic concepts about audio processing and image processing using MATLAB and was used as teaching material for an introductory workshop.

Citation preview

Fun with MATLAB

• A Motivation to Image and Audio Processing using 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.

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

• 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…

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 .

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.

Displaying images

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

using this function…

>> imshow(image);

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.

Audio Processing

Basic Signal Characteristics

Amplitude

Frequency

Phase

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???

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

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

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.

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 !

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.

• 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.

• You may try loading and playing these files too:

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

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

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 ??

How music is recorded?

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.

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???

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);

• 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…

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 ’);

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’);

• 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???

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 );

We think all of it was pretty interesting.

• But, What about you???

• Please, write down your reviews???

Keep Learning. Have Fun.

Thank you…

Recommended