Showing posts with label MATLAB. Show all posts
Showing posts with label MATLAB. Show all posts

Sunday, March 10, 2013

Signal aliasing explored with MATLAB

a·li·as   

/ˈālēəs/

Noun
A false or assumed identity.
So as part of an assignment in a course I'm doing,the task was to explore aliasing.

Just to recap,
"Aliasing is when a signal is not sampled correctly(undersampled with respect to its Nyquist Rate),and this causes the original signal to appear like a lower frequency signal."
What this means is,the frequencies in the signal happen to assume the false identity of lower frequency signals,usually due to very low sampling rates.
This is what most textbooks will tell you.But I never quite felt 'okay' with this definition,till I did this MATLAB exercise.

It might appear trivial,but it has really helped me.Let me walk you through it.

So,to keep things simple,lets consider a 1800Hz tone,which we will sample at 44.1kHz(CD Audio) initially.
ToneFreq=1800;
Fs=44100;
Ts=1/Fs;
t=0:Ts:1-Ts;
Tone=sin(2*pi*ToneFreq*t);


And to get a real idea of things,lets use wavplay command to listen to this tone.Nice and shrill.
wavplay(Tone,Fs);

In terms of the Frequency Spectrum,what we have is this.
Frequency Spectrum for an 1800Hz tone sampled at 44.1kHz

Now the ideal sampling rate according to Harry,is twice the maximum frequency at which the signal has non-zero energy.So that would be 3.6kHz in our case.Which means,anything below this,and we will start seeing the effects of aliasing,and as we go higher away from this(6kHz,8kHz and so on) this effect will die out.

Lets sample at 3kHz,which is a mild case of undersampling.Set Fs=3000.Now when you listen to it,you can observe that the sound is 'duller',or its lowered in frequency,even though the Tone generating function hasnt changed.(ToneFreq still 1800).What happened then? Aliasing. Lets look at the frequency Spectrum now,and this is what you will see:
Frequency Spectrum for an 1800Hz tone 'mildly' under-sampled at 3kHz
 The two peaks are clearly not at 1800,but infact at 3000-1800=1200Hz.So the actual 1800 tone,assumes a false identity of a 1200Hz tone.
The picture in time domain is like this:
1800Hz tone suffering from Aliasing due to mild under-sampling.
The blue line represents the original 1800Hz tone,and the red plot represents the signal when its under-sampled.Clearly,the frequency has got messed up.Sorry,aliased.


This effect is further prominent at even lower sampling frequencies.Lets take 1.5kHz as our sampling frequency,just to make Harry cringe. :P Now we'd see the 1800Hz tone appear like a 1800-1500=300Hz tone.And thats exactly what our plots reflect:
Frequency Spectrum for an 1800Hz tone badly under-sampled at 1.5kHz
The two peaks are at 300Hz.And heres the time domain representation,which makes it clear that the signal is now as good as a 300Hz tone,rather than the 1800Hz one we started with,all due to the effect of sampling rate variation,or in one word - 'Aliasing'.

1800Hz tone suffering from Aliasing due to aggressive under-sampling.

Pretty cool,eh?  Hope this helped you understand aliasing as much as it helped me. :)

I'm not explicitly uploading the MATLAB code,but drop a line in the comments if youre interested.

I was inspired by this other really cool article from the Mathworks blog - http://blogs.mathworks.com/steve/2010/03/03/aliasing-and-a-sampled-cosine-signal/

Thursday, November 3, 2011

Shape Recognition in MATLAB

This post is about something I contributed to a larger Neural Networks project.This isnt a proper tutorial on image processing,and the code discussed is also specific.And in my humble opinion,MATLAB isnt the best choice for Image Processing(OpenCV would be the way to go) anyway.Here it was the only choice since rest of the project was implemented in it.

The idea was to read an RGB Image which has the following characteristics:
-- 400x400px
-- RGB
-- has only one shape,any of a Triangle,Rectangle,Pentagon or a Hexagon,with a solid color fill.
-- has a colored background.

The project required my code to output:
-- The Identity of the shape
-- 2 Images,each of 400x400 px and each with a solid color fill,where one would be of the Shape's fill color and the other of the background color.

So heres my function .m file,all commented.It isnt very clean,and probably not very optimal too,processing wise.Its also not robust(then what use is it? :-/ ) so do tweak it for your specific application.If you spot any areas of improvement,do mention it in the comments below.

I'll briefly explain the shape characterization part,since the rest is pretty much straightforward and already commented.
I've used the Extrema property of the regionprops function and used the relative positions of the various extrem(a)e vertices to identify the shape.See the picture below for a better idea.

So,if we check for the equality(actually closeness of points.Since the left and right points detected will differ by a few pixels,even though the image is perfect.) of the extrema vertices ( whose co-ordinates are stored in the 8x2 Extrema matrix returned by regionprops) we can characterize a shape.

Heres the hexagon case:

Similarly,we can also see what extreme vertices are on the triangle,rectangle and other polygons.

Heres a link to the m-file (hosted on Google Docs)


Drop a line in the comments for any clarification or query.

PS:You can see the output of the different operations on the image using the imshow function.