Chris' Page on Aliasing using MatLab: Difference between revisions

From Class Wiki
Jump to navigation Jump to search
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 21: Line 21:
'''Matlab Program'''
'''Matlab Program'''


<pre>
% Chris Rasmussen
% Chris Rasmussen
% Based on Matlab examples


% Show aliasing
% Show aliasing



clc
clc

%****************%
%****************%

% Parameters
% Parameters

duration = 8
duration = 8

fmax = 20000
fmax = 20000

fmin = 20
fmin = 20

samplerate = 8000
samplerate = 8000

%****************%
%****************%



%****************%
%****************%
Line 48: Line 39:
% Will still work if this is removed, you just won't hear anything
% Will still work if this is removed, you just won't hear anything
% It works with my onboard soundcard with Vista.
% It works with my onboard soundcard with Vista.

AO = analogoutput('winsound');
AO = analogoutput('winsound');

chan = addchannel(AO,1);
chan = addchannel(AO,1);

set(AO,'SampleRate',samplerate);
set(AO,'SampleRate',samplerate);

set(AO,'TriggerType','Manual');
set(AO,'TriggerType','Manual');

ActualRate = get(AO,'SampleRate')
ActualRate = get(AO,'SampleRate')

%****************%
%****************%



%****************%
%****************%

% Setup arrays
% Setup arrays

totalsamples = ActualRate*duration;
totalsamples = ActualRate*duration;

t = 0:1./ActualRate:duration;
t = 0:1./ActualRate:duration;

y= 0.2 .* chirp(t,fmin,duration, fmax,'q',[],'concave');
y= 0.2 .* chirp(t,fmin,duration, fmax,'q',[],'concave');

%****************%
%****************%



%****************%
%****************%

% Write data to soundcard and play
% Write data to soundcard and play

% Will still work if this is removed, you just won't hear anything
% Will still work if this is removed, you just won't hear anything

putdata(AO,y')
putdata(AO,y')

start(AO)
start(AO)

trigger(AO)
trigger(AO)

waittilstop(AO,duration+1)
waittilstop(AO,duration+1)

%****************%
%****************%



%****************%
%****************%

% Plot raw data
% Plot raw data

figure(1)
figure(1)

plot(y)
plot(y)

title('Waveform')
title('Waveform')

%****************%
%****************%



%****************%
%****************%

% Plot spectragram (uses FFT)
% Plot spectragram (uses FFT)

figure(3), specgram(y, 256, samplerate);
figure(3), specgram(y, 256, samplerate);

title('Quadratic Sine Sweep')
title('Quadratic Sine Sweep')

set(gcf,'Color',[1 1 1]);
set(gcf,'Color',[1 1 1]);

%****************%
%****************%


Line 120: Line 79:


%****************%
%****************%

% Picking up the messes
% Picking up the messes

delete(AO)
delete(AO)

clear AO
clear AO

clear all
clear all
</pre>

Latest revision as of 10:53, 12 November 2007

Demo of Aliasing Using Matlab

Frequencies above half the sample rate will produce aliasing. This should appear as a lower frequency signal.


Below is a spectrogram of a quadratic sweep from 20 Hertz to 20,000 Hertz using a 441000 Hertz sample rate.


Chirp41000b.jpg

Media:44100hz.ogg


This one is the same script and parameters except the sample rate has been changed to 8000 Hertz.

Chirp8000b.jpg

Media:8000hz.ogg


Matlab Program

% Chris Rasmussen
% Based on Matlab examples
% Show aliasing

clc
%****************%
% Parameters
duration = 8
fmax = 20000
fmin = 20
samplerate = 8000
%****************%

%****************%
% Setup Soundcard 
% Will still work if this is removed, you just won't hear anything
% It works with my onboard soundcard with Vista.
AO = analogoutput('winsound');
chan = addchannel(AO,1);
set(AO,'SampleRate',samplerate);
set(AO,'TriggerType','Manual');
ActualRate = get(AO,'SampleRate')
%****************%

%****************%
% Setup arrays
totalsamples = ActualRate*duration;
t = 0:1./ActualRate:duration; 
y= 0.2 .* chirp(t,fmin,duration, fmax,'q',[],'concave');
%****************%

%****************%
% Write data to soundcard and play
% Will still work if this is removed, you just won't hear anything
putdata(AO,y')
start(AO)
trigger(AO)
waittilstop(AO,duration+1)
%****************%

%****************%
% Plot raw data
figure(1)
plot(y)
title('Waveform')
%****************%

%****************%
% Plot spectragram (uses FFT)
figure(3), specgram(y, 256, samplerate);
title('Quadratic Sine Sweep')
set(gcf,'Color',[1 1 1]);
%****************%

wavwrite(y,samplerate,16,'File.wav')

%****************%
% Picking up the messes
delete(AO)
clear AO
clear all