MATLAB Answers

0

Unknown Error

Asked by Jonathan on 31 May 2012
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) % y is sound data, Fs is sample frequency.
t = (1:length(sound))/Fs; % time
ind = find(t>0.1 & t<0.12); % set time duration for waveform plot
figure; subplot(1,2,1);
plot(t(ind),sound(ind));
axis tight
title(['Waveform of ' file]);
xlabel('time, s');
N = 2^12; % number of points to analyze
c = fft(sound(1:N))/N; % compute fft of sound data
p = 2*abs( c(2:N/2)); % compute power at each frequency
f = (1:N/2-1)*Fs/N; % frequency corresponding to p
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; % frequency and bit rate of wav file
t = linspace(1/Fs, d, d*Fs); % time
y = zeros(1,Fs*d); % initialize sound data
for n=1:length(sound);
y = y + sound(n).*cos(2*pi*n*f*t); % sythesize waveform
end
y = .5*y/max(y); % normalize. Coefficent controls volume.
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

Geoff
on 31 May 2012
On which line is the error? By "second function" do you mean synth3? You haven't shown the line of code that calls that function. Are you requesting more than 2 outputs from it?
Jonathan on 31 May 2012
The erro is on the line "for n=1:length(sound);". Yes, synth3 is the second function. [y, Fs] = synth3(file,f,d) calls the function. I am requesting only 2 outputs: y and Fs

Sign in to comment.

2 Answers

Walter Roberson
Answer by Walter Roberson
on 31 May 2012

Unless your second function is nested inside the first, it has no access to the "sound" array you define inside the first function (because you do not pass the array to the second function.) So inside the second function instead of accessing the array it doesn't know about, it looks around and finds the sound() function and in the process of setting up to run it, notices that sound() does not return any outputs but the context requires that sound returns a value...
We warn people about naming their variables the same thing as MATLAB functions...

  15 Comments

Walter Roberson
on 1 Jun 2012
That tells you that it was executing that line when you interrupted the program. You have asked it to do so much work that it is not finishing before you interrupt it. Consider using waitbar() to monitor the progress.
Oleg Komarov on 1 Jun 2012
@Jonathan: it's not amusing to play the discovery game. Please supply all the relevant information at once specifying the context. Were you planning to tell us "when I kill the program"? Or just the right combination of questions will unlock that achievement?
Consider my answer from a slightly humouristic point.
Walter Roberson
on 1 Jun 2012
MATLAB responds to control-C by printing out the line that it was executing at the time you interrupted. The "error" it reports for this purpose is the control-C interruption itself, not an error in the line it prints out.
What value is d*Fs*length(sound) ?

Sign in to comment.


Stephen
Answer by Stephen
on 31 May 2012

if the function usually outputs 1 thing and you ask for more, it will give you that error. for example,
function ans = myfunc(x,y)
ans = x + y;
end
will error when I write:
[ans1, ans2] = myfunc(1,1);

  5 Comments

Jonathan on 31 May 2012
sorry i meant "synth3(file,f,d)"
Ryan
on 1 Jun 2012
what are your outputs? [outputs] = function(inputs)
Jonathan on 1 Jun 2012
See below. This question was solved by Walter Roberson. However, now I am getting an error at "y = y + sound(n)*cos(2*pi*n*f*t);"

Sign in to comment.