Unknown Error

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

Geoff
Geoff 2012년 5월 31일
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
Jonathan 2012년 5월 31일
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

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2012년 5월 31일

1 개 추천

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

Jonathan
Jonathan 2012년 5월 31일
Oh, ok. So how do I pass the array to the second function?
Walter Roberson
Walter Roberson 2012년 5월 31일
In your duplicate question (which I have just deleted for the sake of sanity), you indicate
function [sound, Fs] = analyzer(file)
This indicates that you return "sound" and "Fs" when you run the routine. So assign those outputs to variables when you run analyzer():
[sound, Fs] = analyzer('handle.wav');
Now you have sound and Fs ready to pass into synth3 as additional arguments:
synth3('TheOutputFile.wav', 12345, 3.14, sound, Fs)
You would have to adjust the "function" line of synth3() to expect these as inputs. And you probably don't want to overwrite Fs within the synth3() routine like you do now...
Jonathan
Jonathan 2012년 5월 31일
Wow. Thanks a bunch! However, I am now getting an error in "y = y + sound(n)*cos(2*pi*n*f*t);"
Jonathan
Jonathan 2012년 5월 31일
I'm not sure why I keep on getting this error.
Ryan
Ryan 2012년 6월 1일
what is there error name? If both sound(n) and cos(A*t) are vectors, you'll want to use 'sound(n).*cos(2*pi*n*f*t)'. This way, instead of trying to multiply the two vectors using matrix algebra, it multiplies each matching array pair, e.g. results = [sound(1) x cos(A*1), sound(2) x cos(A*2)... sound(n) x cos(A*n)].
Jonathan
Jonathan 2012년 6월 1일
I just tried this, and I am still getting the error.
Jonathan
Jonathan 2012년 6월 1일
And there is no error name.
Oleg Komarov
Oleg Komarov 2012년 6월 1일
Please post the exact way you're calling the function with example inputs.
Ryan
Ryan 2012년 6월 1일
also please copy the red text exactly that matlab spits back at you.
Walter Roberson
Walter Roberson 2012년 6월 1일
At the command line command
dbstop if error
then run your program. When it stops it will indicate the error. You can then examine the size of all of the variables and experiment with sub-expressions until you figure out what it is complaining about and why.
Jonathan
Jonathan 2012년 6월 1일
I tried this, but it never stops. It's just busy forever.
Jonathan
Jonathan 2012년 6월 1일
When I kill the program, it tells me where the error is, but not what it is. I can't figure out what wrong with: y = y + sound(n).*cos(2*pi*n*f*t); % sythesize waveform
end
Walter Roberson
Walter Roberson 2012년 6월 1일
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
Oleg Komarov 2012년 6월 1일
@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
Walter Roberson 2012년 6월 1일
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) ?

댓글을 달려면 로그인하십시오.

Stephen
Stephen 2012년 5월 31일

0 개 추천

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

Jonathan
Jonathan 2012년 5월 31일
even when I write just "synth3 = (file,f,d)", I still get the error.
Geoff
Geoff 2012년 5월 31일
synth3 is a function. why are you using it as if it's a variable?
Jonathan
Jonathan 2012년 5월 31일
sorry i meant "synth3(file,f,d)"
Ryan
Ryan 2012년 6월 1일
what are your outputs? [outputs] = function(inputs)
Jonathan
Jonathan 2012년 6월 1일
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);"

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

제품

질문:

2012년 5월 31일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by