2

Below you can see a function which generates pure sinusoidal waveform. I need to modify it to generate sawtooth waveform. Help please! (I'm on Matlab 2014b with Psychtoolbox 3.0.12, Win 8.1 x64)

function [tSnd] = mkPureTone(fs, frq, dur)

if nargin < 1; fs = 44100; end
if nargin < 2; frq = 1000; end
if nargin < 3; dur = 0.1; end


rtimeup = 0.006;    % duration of the ramp up in seconds
rtimedown = 0.006;   % duration of the ramp down in seconds
trup = 0:1/fs:rtimeup-1/fs;
lrup = length(trup);
rampup = (cos(2*pi*trup/rtimeup/2+pi)+1)/2;
trdown = 0:1/fs:rtimedown-1/fs;
lrdown = length(trdown);
rampdown = (cos(2*pi*trdown/rtimedown/2)+1)/2;

% compute target sound
time = 0:1/44100:dur;

sound = sin(2*pi*frq*time);
lt = length(sound);
sound(1:lrup) = sound(1:lrup).*rampup;
sound(lt-lrdown+1:end) = sound(lt-lrdown+1:end).*rampdown;
tSnd = sound;

% wavplay(tSnd, fs);

end
Bartosz M
  • 123
  • 8

2 Answers2

1

The simple way to go would be to replace

sound = sin(2*pi*frq*time);

with something like

sound = mod(frq*time,1);

This will create a sawtooth at frequency frq and of amplitude 1 (i.e. between 0 and 1). You can change the amplitude of the waveform (by multiplying it with a scalar) or shift it (by adding a scalar), depending on your needs.

But this modification won't take into account the rising and falling ramps. The way it is handled in the code you show seems rather strange to me, especially I don't get why one needs to use the cos function, so I don't touch that part of the code.

Best,

Ratbert
  • 5,463
  • 2
  • 18
  • 37
0

Thanks. Colleague of mine also looked into it and his idea was:

sound = time*frq - floor(time*frq)

Both this and your version sound indistinguishably, but when I compare it to the sawtooth wav generated online, this Matlab sound sounds less clear and it begins and ends with small but audible click. Not sure if it's a matter of driver or of the generated sound itself.

Bartosz M
  • 123
  • 8