Friday, September 18, 2009

Matlab: Sound generation

Pure tone: unmodulated sinusoid, matlab function:

% Ft: frequency of the tone
% Fs: sample rate
% Td: last seconds
% Output is a row vector s containing the sampled point
% t is an optional and is the time axis associated with s

function [s, t] = tonegen(Ft, Fs, Td)
t = [1:Fs*Td]/Fs;
s = sin (2 * pi * t * Ft);

% example:
y = tongen(440, 16000, 5);
soundsc(y, 16000);

Generate sound with different frequency:

% frc: time-varying frequency
% Fs: sample rate

function [snd]=freqgen(frc, Fs)
th=0;
fr=frc*2*pi/Fs;
for si=1:length(fr)
th=th+fr(si);
snd(si)=sin(th);
th=unwrap(th);
end

% example
>> freq=[440*(1+zeros(1,1000)),415.2*(1+zeros(1,1000)),392*(1+zeros(1,1000))];
>> music=freqgen(freq, 8000);
>> soundsc(music, 8000);

No comments:

Post a Comment