# Creating 2D array to play audio in left and right channel in matlab

I'm new to matlab sound processing and I am trying to create a 2D array for the sound code below. I have to make the sound have 400 samples(i.e 10ms) at 20hz, but having trouble getting the code to work. Any help would be greatly appreciated. The steps are: (1) Construct a two-dimensional array (n x 2 array, i.e., 2 columns, n rows, n is the number of samples per sound channel). The array is the sound signal, the first column is the sound for the left channel (left speaker), the second column is the sound for the right channel (right speaker) (2) Then play the sound.

Fs = 44100;
t = [0:4/Fs:4-4/Fs];
freq = 20;
f1 = sin(2*pi*freq*t);
f2 = sin(2*pi*2*freq*t);
%Sound 1
sound(f1,Fs);
%Sound 2
sound(f2,Fs);
%Play Consecutive
f12 = [f1 f2];
sound(f12,Fs);
%Play together
f_12 = [f1+f2];
sound(f_12,Fs);


Since 20 Hz was a little hard to hear with my speakers, I've changed the main frequency to 600 Hz. I guess it should work:

clc;
clear all;

Fs = 44100;
N = 40000; % sample size
t = (1:N)*(1/Fs);
freq = 600;

sound_array = zeros(N, 2); % initializes a N x 2 matrix
sound_array(:,1) = sin(2*pi*freq*t);
sound_array(:,2) = sin(2*pi*2*freq*t);

% Sound 1
sound(sound_array(:,1),Fs);

% Sound 2
sound(sound_array(:,2),Fs);

% Play the first column at left channel and the second column at the right
% channel
sound(sound_array,Fs);


In fact, if you use the entire array as input in sound, the first column will go to the left channel and the second column to the right channel.

• Playing the sound wasn't the issue...how would I create the 2D array for the left and right channel? – Asau7610 May 14 '14 at 13:43
• @Asau7610, sorry! I've just edited the answer. – lucasn May 14 '14 at 13:54
• I keep getting an error saying " attempt to execute script sound as a function...error in Soundcreate (line 14) sound(array(:,1,Fs); – Asau7610 May 14 '14 at 15:25
• Also @lucasn what does the N = 4000 stand for. And the 20hz is being played so that just outsid human hearing but a recorder object can still record and read the sound. – Asau7610 May 14 '14 at 15:41
• N is the sample size. The vector t contains N time samples acquired at 44100 Hz. Then we use it to obtain the sinusoids samples. – lucasn May 14 '14 at 15:47

I'd start at the construction of the vector t. The assignment says to generate a signal with a length of 400 samples. However, due to the way you constructed t, your signals have a length of 44100 samples. In the way I'd recommend to generate signals we need a vector that contains the indices of the samples our signal will eventually have, so

t = (0:399)';


This gives us a column vector (because I used the transpose operator ') of size 400x1, containing the indices of the samples of our signal-to-be.

Now your assignment says to generate a signal with a frequency of 20 Hz and let it run for 400 samples. I think that is a little bit odd, since a signal of that frequency does not complete a full cycle in only 400 samples with a sampling rate of 44100 Hertz. Anyway, we will continue as asked of us by your assignment.

We now have to convert the frequency of 20 Hertz to the angular frequency that depends on Fs. To do that, we use the following piece of code:

w = 2*pi*(f/Fs);


with f = 20;. Together with t, w will now be our input for the sin() function to generate the signal. This is now trivial:

f1 = sin(w*t);


The next part of the exercise seems to be to combine two mono signals of the kind we just created into a stereo signal.

(1) Construct a two-dimensional array (n x 2 array, i.e., 2 columns, n rows, n is the number of samples per sound channel).

Do achieve that, you should look into how vectors (or matrices) are concatenated in MATLAB. Since f1 and f2 are column vectors, to combine them into a matrix of size 400x2, we can use

F12 = [f1 f2];


If we wanted to concatenate them, like you have also done in your code, we would have to use

F12 = [f1; f2];


In order to play back these signals, I'd recommend using soundsc(), which normalizes the audio before playback (so it doesn't damage your speakers in case of high amplitudes):

soundsc(F12, Fs);