This code gives me a sound vector and Fs scalar from a .wav sample and plots the waveform and power spectrum:
function [sound, Fs] = analyzer(file)
[sound, Fs] = wavread(file)
t = (1:length(sound))/Fs;
ind = find(t>0.1 & t<0.12);
figure; subplot(1,2,1);
plot(t(ind),sound(ind));
axis tight
title(['Waveform of ' file]);
xlabel('time, s');
N = 2^12;
c = fft(sound(1:N))/N;
p = 2*abs( c(2:N/2));
f = (1:N/2-1)*Fs/N;
subplot(1,2,2);
semilogy(f,p);
axis([0 4000 10^-4 1]);
title(['Power Spectrum of ' file]);
xlabel('frequency, Hz');
----------------------------------------------------------------------------------------------------------------------------------
This code is supposed to write and play a .wav file from the "sound" data above with a specified fundamental frequency and duration:
function guitarSynth(file,f,d,sound,Fs)
nbits=8;
t = linspace(1/Fs, d, d*Fs);
y = zeros(1,Fs*d);
for n=1:length(sound);
y = y + sound(n).*cos(2*pi*n*f*t);
end
y = .5*y/max(y);
wavwrite( y, Fs, nbits, file)
wavplay(y,Fs)
------------------------------------------------------------------------------------------------------------------------------------
The "too many output arguments" error has been solved. The problem is that MATLAB is busy forever, and I am forced to kill the program. When I hit Ctrl+C to kill the program, MATLAB says there is an error at "y = y + sound(n)*cos(2*pi*n*f*t);". I don't see what the error is. Please help.
2 Comments
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/40022-unknown-error#comment_82615
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/40022-unknown-error#comment_82615
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/40022-unknown-error#comment_82619
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/40022-unknown-error#comment_82619
Sign in to comment.