Skip to content Skip to navigation

Connexions

You are here: Home » Content » MATLAB EQ: Sound Sampling

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

      A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

      What is in a lens?

      Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

      Who can create a lens?

      Any individual Connexions member, a community, or a respected organization.

    • External bookmarks
  • E-mail the author

Recently Viewed

MATLAB EQ: Sound Sampling

Module by: Chris Corbet

Summary: 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)

Comments, questions, feedback, criticisms?

Send feedback