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);