Azzera filtri
Azzera filtri

Vectors and Scalars in function calls

3 visualizzazioni (ultimi 30 giorni)
C Meek
C Meek il 15 Feb 2012
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 Commento
Honglei Chen
Honglei Chen il 15 Feb 2012
MATLAB can definitely handle that. Can you show the error?

Accedi per commentare.

Risposta accettata

Wayne King
Wayne King il 15 Feb 2012
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);

Più risposte (2)

Wayne King
Wayne King il 15 Feb 2012
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 il 15 Feb 2012
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

Community Treasure Hunt

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

Start Hunting!

Translated by