0
$\begingroup$

I'm trying to create cosine wave, one for the right channel and another for the left. The one on the left is to be moved in phase to simulate change to the spatial perception of the tone. A delay in time to the left side makes us think the sound comes from the right, and since time is converted to phase in a cosine I am working with the phase. I wish to use a $500\textrm{ Hz}$ cosine and a $2\textrm{ mSec}$ signal. MT ultimate goal here is to have 9 sounds to simulate the locations around a person's head. 1-4 being left, 5 the center and 6-9 the right. This a snippet of the code responsible for this:

t = [ 0 : 1 : 2000 ];   % Time Samples
f = 500;    % Input Signal Frequency 500Hz
fs = 44100; % Sampling Frequency

x =  cos(2 * pi * f / fs * t);  % Generate Sine Wave  

y =  cos(2 * pi * f / fs * t - DELAY);  % Generate Sine Wave  

Y = [x,y];

audiowrite(['file' num2str(j) '.wav'], Y, fs);

The issue I'm having is that no matter what values I try for DELAY the resulting file sounds just like any other, I cannot hear any change. What am I doing wrong?

$\endgroup$
1
$\begingroup$

Nothing is wrong, apart from assuming that the delayed $\cos$ extends to infinity both towards negative and positive time. In other words, the delayed version of the cosine starts 2msec after the non-delayed one. Therefore, before that time, there is supposed to be silence (and complete silence is not part of the cosine as we know it).

Theoretically, you could work with a cosine that starts at $t=0$ at some frequency $f$ for both channels, but, modulate the cosine with a step function to control when the cosine becomes audible in either channels. In that case, one of the channels would have a step at $t=0$ and the other at $t=2$msec. The timing would be controlled by the timing between the pulses.

But, a less "messy" way to achieve the same thing is to generate one cosine at some frequency $f$ and simply offset it by a number of samples.

So:

Fs=44100;
#Time
t = 0:(1./Fs):(1-(1./Fs));
#Phase
p = 2.*pi.*t;
#A signal
s = cos(500.*p)
#The delay in samples
ds = round(0.002./(1./Fs))
#Stereo Sound with shifted versions, the shifted version includes a number of zeros the space of which (in time) is "trimmed" from the actual sound
#Please also note here that Q is an Nx2 matrix where Q(:,1) is the left channel and Q(:,2) is the right channel.
Q=[s';[zeros(1,ds), s(1:(end-ds)]']
#Let's hear it
soundsc(Q,Fs)

Finally, if you are using the $\cos$ trigonometric function, you are going to have relatively audible "clicks", because the $\cos$ starts from $1$. So, the first thing that the speaker has to do to reproduce the sound is to push the cone all the way out to start from that $1$. It will be better to use the $\sin$.

$\endgroup$
1
$\begingroup$

Try this

DELAY = 0.5e-3; % half a millisecond for starters. 
t = [ 0 : 1 : 2000 ]';   % Time Samples, COLUMN vector
f = 500;    % Input Signal Frequency 500Hz
fs = 44100; % Sampling Frequency
x =  cos(2 * pi * f / fs * t);  % Generate Sine Wave
y =  cos(2 * pi * f * ( t/fs - DELAY));  % Generate Sine Wave
Y = [x,y];

A bunch of problems with your code

  1. Your variable t was a row vector so you generated a mono output signal
  2. The units of your DELAY variable are unclear. I have written it in a way so that it is in actual seconds.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.