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

조회 수: 1 (최근 30일)
Spencer
Spencer 2012년 4월 6일
답변: sing lai 2014년 2월 23일
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에서 Using audio files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by