Function to create a sound --> (Error: Too many Input Arguments?)

2 次查看(过去 30 天)
So, I am required to creat a function that displays a simple tone, based on a numerical value corresponding to a key number, a duration, and an amplitude.
After messing around a bit, I came up with this. This does in fact display a sound, but I do not know if it is correct.
function x = note(keynum, dur, A)
fs = 44100; % sampling frequency
freq=440*2^((keynum-49)/12);
t = 0:1/fs:dur;
x = A*sin(2*pi*freq*t);
plot(t,x);
sound(x,fs)
While I think this could be correct, I am confused as to the purpose of an "(sine wave?)" equation we are required to use.
That equation is apparently:
%x(t) = (A)sin(2 pi f t) + (A/3)sin(2 pi(3f)t) + (A/5)sin(2 pi(5 f)t) + (A/7)sin(2 pi(7f)t)
I assume this is some sort of way of creating a more pure and accurate sound, but am not sure.
I tried replacing this equation with the line %x = A*sin(2*pi*freq*t); in my first function( at Line marked with (%%%), resulting in a new function:
function x = note(keynum, dur, A)
fs = 44100; % sampling frequency
freq=440*2^((keynum-49)/12);
t = 0:1/fs:dur;
x(t) = (A*sin(2*pi*(freq)*t)) + ((A/3)*sin(2*pi*(3*freq)*t)) + ((A/5)*sin(2*pi(5*freq)*t)) + ((A/7)*sin(2*pi*(7*freq)*t)); %%%
plot(t,x);
sound(x,fs)
So, I figured I had this all figured out, but when I call the function with, for example a Middle C tone, duration of 1, and an amplitude of 2, %x=note(40,1,2), I get an error:
??? Error using ==> pi
Too many input arguments.
Error in ==> note at 5 (Line 5 is marked with %%%)
The first function gave a response, but the new one doesn't.
So, I have no idea if I did EVERYTHING wrong in regards to my function and the usage of the (sine wave?) equation is wrong, or its a simple mistake I am not catching.
Any help or pushes in the right direction would be greatly appreciated. I can try to provide more details if needed. Thanks in advance.

采纳的回答

Walter Roberson
Walter Roberson 2012-4-6
You coded
pi(5*freq)
when you meant
pi*(5*freq)
  3 个评论
Spencer
Spencer 2012-4-6
Never mind, got it,Somehow I left the (t) in when assigning the equation to x.
Thanks again!
Walter Roberson
Walter Roberson 2012-4-6
Your t are not integer, but you are trying to store into x(t). You should just be storing to x, not x(t)

请先登录,再进行评论。

更多回答(1 个)

sing lai
sing lai 2014-2-23
I try to use your code, but there is error in this line: freq=440*2^((keynum-49)/12) why?

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by