Function to create a sound --> (Error: Too many Input Arguments?)
2 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Walter Roberson
on 6 Apr 2012
You coded
pi(5*freq)
when you meant
pi*(5*freq)
3 Comments
Walter Roberson
on 6 Apr 2012
Your t are not integer, but you are trying to store into x(t). You should just be storing to x, not x(t)
More Answers (1)
sing lai
on 23 Feb 2014
I try to use your code, but there is error in this line: freq=440*2^((keynum-49)/12) why?
0 Comments
See Also
Categories
Find more on Using audio files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!