필터 지우기
필터 지우기

Vectors and Scalars in function calls

조회 수: 3 (최근 30일)
C Meek
C Meek 2012년 2월 15일
Hi there,
I'm making another reverb function, where I'd like the input to include room dimensions (L, a 1x3 vector) and a scalar, N.
For the vector, I then extract the length of each dimension (Lx=L(1), Ly=L(2) etc) in the following code. The problem is, I have tried making this function, is_reverb(L,N) accept these values and I keep getting errors.
So I guess my question is if it is possible to do a function call taking a vector for one variable and a scalar for another?
  댓글 수: 1
Honglei Chen
Honglei Chen 2012년 2월 15일
MATLAB can definitely handle that. Can you show the error?

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

채택된 답변

Wayne King
Wayne King 2012년 2월 15일
That's correct Craig, you save that imagesource.m file somewhere on the MATLAB search path then define your L vector in the workspace as well as your N
>> L = [20 10 8];
>> N = 50;
and then call
>>h =imagesource(N,L);

추가 답변 (2개)

Wayne King
Wayne King 2012년 2월 15일
You can do definitely do that. It would help a lot if you posted the code (using formatting) and the errors you are getting.

C Meek
C Meek 2012년 2월 15일
Certainly.
function [h]=imagesource(N,L);
Lx=L(1);Ly=L(2);Lz=L(3); %Room Dimensions
S=[50 50 25];
p=S(1); q=S(2); r=S(3); %Source
R=[1 1 1.8];
a=R(1); b=R(2); c=R(3); %Receiver
alpha=0.8; %absorption coefficient
%N=10;
Fs=44100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tf=sqrt((N*Lx+p-a)^2+(N*Ly+q-b)^2+(N*Lz+r-c)^2)/343; %last arriving impulse
h=zeros(1,ceil(tf*Fs)); %impulse response vector
for d=-N:N
if mod(d,2)==1
A_d=(d+1)*Lx-p-a;
else
A_d=d*Lx+p-a;
end
for e=-N:N
if mod(e,2)==1
B_e=(e+1)*Ly-q-b;
else
B_e=e*Ly+q-b;
end
for f=-N:N
if mod(f,2)==1;
C_f=(f+1)*Lz-r-c;
else
C_f=f*Lz+r-c;
end
l_def=sqrt((A_d^2+B_e^2+C_f^2)); %distance
w=abs(d)+abs(e)+abs(f); %number of reflections
g=(1/l_def)*alpha^w; %magnitude
t=l_def/343; %time
k=ceil(t*Fs);
h(k+1)=g; %updating impulse response vector
end
end
end
plot(h);
wavwrite(h,Fs,'h');
disp('Impulse Response File Write Successful!')
end
Sorry for the length, but I wouldn't worry too much about the stuff in the for loops. As you can probably deduce I have a similar ideas for the vectors S and R - I just haven't tried to move them into the function call yet.
Actually, I've just run the code again and got no errors so I think I'm ok. So you simply pre-define a vector and scalar and just read them into the function call as normal?
Thanks
Craig

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by