<< Chapter < Page Chapter >> Page >
This module outlines how we access and store analog data in MATLAB.

Sampling windows audio data in matlab

This module will walk you through the coding process of saving input audio data from a Windows sound card.

Setup

The first thing you need to do is set how often you would like MATLAB to access your sound card, and how fast you want it to do so. This multiplying these two values together will give you the total size of your sample space. The code below is how we initialized our data input.

refreshrate = .04644; % sec

samplerate = 44100; % Hz

ai = analoginput('winsound', 1); %windows addchannel(ai,[1 2]); %two channels

samplerate = setverify(ai, 'SampleRate', samplerate);

Trigger

Next, you must set a command structure to let MATLAB know when to access the sound data. In the case of our graphical equalizer we wanted MATLAB to sample until we told it to stop so we created an infinite trigger loop. To do this you must set the triggers on your input class.

ai.TimerPeriod = refreshrate;

spt = round(samplerate * refreshrate);

ai.SamplesPerTrigger = spt;

set(ai, 'TriggerRepeat', Inf);

set(ai, 'TimerFcn' , @getdata);

start(ai);

Storing data and stopping

Now that you have begun to sample the soundcard, you have to store the sampled data in a buffer for analysis. You need to flush the data in your acquisition structure or you will suffer serious memory leaks. Also, the try and catch structure allows the loading of empty values if the peekdata is empty (a type of error suppression).

try

timesig = peekdata(ai,samples);

flushdata(ai)

catch

timesig = [];

end

Stopping the data acquisition is simple:

stop(ai)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Elec 301 projects fall 2007. OpenStax CNX. Dec 22, 2007 Download for free at http://cnx.org/content/col10503/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Elec 301 projects fall 2007' conversation and receive update notifications?

Ask