Well first of all, your y is a single number - a scalar - not a complete waveform as it should be when passed into soundsc(). Then you need to put the soundsc() outside of the loop so it plays the whole waveform, not just one number. next, your formula for the cosine wave is not right. It should be cos(2 * pi * omega * time).
Here's some more. Zoom in on the y plot so you can see it has the correct time-varying frequency.
Num=[40 42 44 40 0 40 42 44 40 0 44 45 47 0 44 45 47 0];
dur= 2.3;
fs=48000;
A=1;
t = 0:1/fs:dur;
Num = interp1(0:length(Num)-1, Num, t, 'nearest');
f = zeros(length(Num),0);
y = zeros(length(Num),0);
for i=1:length(Num)
f(i) = 440*2^(((Num(i))-49)/12);
y(i) = A*cos(2*pi*f(i)*i);
end
subplot(3, 1, 1);
plot(Num, 'r-');
grid on;
subplot(3, 1, 2);
plot(f, 'g-');
grid on;
subplot(3, 1, 3);
plot(y, 'b-');
grid on;
soundsc(y,fs)
Of course you could vectorize it and get rid of the for loop like this:
f = 440 * 2 .^ ((Num-49)/12);
y = A*cos(2 * pi * f .* i);
See my attached script that creates a signal and makes a wav file out of it. Adapt as needed.
0 Comments
Sign in to comment.