Main Content

Generate Audio Signals

This example shows how to generate audio signals using a 5.1 channel sound system.

Load Audio Signal

Load an audio file containing a sample of Handel's "Hallelujah Chorus."

load handel;

Plot Audio Signal

Plot the data to identify five distinct segments. Each segment represents a "Hallelujah" in the chorus. The segments are annotated as 1 to 5.

ly = length(y);
lspan = 1:ly;
t = lspan/Fs;

hf = figure;
plot(t,y./max(y))
axis tight;
title("Signal (Handel''s Hallelujah Chorus) vs Time");
xlabel("Time (s)");
ylabel("Amplitude");

markers = struct('xpos',[0.2,0.4,0.55,0.65,0.8],'string',num2str([1:5]'));
for i = 1:5,
    annotation(hf,'textbox',[markers.xpos(i) 0.48 0.048 0.080],'String', markers.string(i),'BackgroundColor','w','FontSize',16);
end

Create DataAcquisition Object and Add Audio Output Channels

This example uses a 5.1 channel sound system with device ID 'Audio2'.

1. Create a DataAcquisition object with directsound as the vendor and add an audio output channel to it using the addoutput function.

dd = daq("directsound");
nch = 6;
addoutput(dd, "Audio2", 1:nch, "Audio");

2. Update the generation scan rate to match the audio sampling rate.

dd.Rate = Fs;

3. Generate audio signals (Handel's "Hallelujah Chorus") using the write function. "Hallelujah" should be voiced five times, one for each segment depicted in the figure on all channels of the speaker system.

write(dd, repmat(y,1,nch));

4. Close the figure.

close(hf);