Code covered by the BSD License  

Highlights from
MATLAB para Programadores C/C++

image thumbnail
from MATLAB para Programadores C/C++ by Elia Matsumoto
Webinar's slides and demo files.

soundcard( )
function soundcard( )
%% Define Soundcard input
ai = analoginput('winsound');
addchannel(ai, 1);

%% Configure
% Now, let's set up the analog input object so that we acquire 5 seconds of 
% data at 8000 Hz as soon as the object is started. 
Fs=8000;
Ns=800;
ai.SampleRate = Fs;
ai.SamplesPerTrigger = Ns;
ai.TriggerType = 'Immediate';
ai.TriggerRepeat = Inf;

%% Start acquisition
start(ai)
pause(0.2)

%% Collect and plot
% Each time you execute this cell, new data is acquired and plotted. Dock
% the figure in the desktop for best results.
[d] = peekdata(ai,Ns);
plot(d);
ylim([-.25 .25])

%% Analyze and plot
% Each time you execute this cell, new data is acquired, fft calculated and
% plotted. Dock the figure in the desktop for best results.
d = peekdata(ai,Ns);
y=abs(fft(d)).^2;
f=(0:Fs/Ns:Fs/2-1);
semilogy(f,y(1:length(y)/2));
ylim([1e-5 1e3]);

%% Put analysis and plotting in loop, fix and annotate axis
for k= 1:200
    d = peekdata(ai,Ns);
    y=abs(fft(d)).^2;
    semilogy(f,y(1:length(y)/2));
    axis tight;
    ylim([1e-5 1e3]);grid;
    ylabel('Amplitude');
    xlabel('Frequency');
    drawnow;
end;

%% Clear up
stop(ai);
ai.SamplesPerTrigger=8000;
delete(ai);

Contact us at files@mathworks.com