MATLAB简化循环

时间:2014-12-13 04:58:30

标签: matlab for-loop

好的,所以我正在努力解决这个问题。下面的代码适用于我如何需要它,但是,我确信它可以更短更简单。我知道我可以使用for循环以缩短它我只是不知道如何,任何人都可以指出我正确的方向这样做。谢谢:))

如果代码不清楚我正在尝试做什么。我试图使用除卷积函数之外的其他过程将单独的信号和房间脉冲响应一起卷积

close all;
clear all;

[x Fs] = wavread('bass.wav');
[x2] = wavread('drums.wav');
[x3] = wavread('bongo.wav');
[x4] = wavread('shaker.wav');
[x5] = wavread('gtr.wav');

hrir = wavread('HRIR_RR.wav');
hrir2 = wavread('HRIR_RL.wav');
hrir3 = wavread('HRIR_FR.wav');
hrir4 = wavread('HRIR_FL.wav');
hrir5 = wavread('HRIR_C.wav');

hrir_L = hrir(:,1);
hrir_R = hrir(:,2);

hrir_L2 = hrir2(:,1);
hrir_R2 = hrir2(:,2);

hrir_L3 = hrir3(:,1);
hrir_R3 = hrir3(:,2);

hrir_L4 = hrir4(:,1);
hrir_R4 = hrir4(:,2);

hrir_L5 = hrir5(:,1);
hrir_R5 = hrir5(:,2);

L = length(x) + length(hrir) -1; % Output data length
X = fft([x ; zeros(length(hrir_L),1)]);
HL = fft([hrir_L ; zeros(length(x),1)]);
HR = fft([hrir_R ; zeros(length(x),1)]);

L2 = length(x2) + length(hrir2) -1; % Output data length
X2 = fft([x2 ; zeros(length(hrir_L2),1)]);
HL2 = fft([hrir_L2 ; zeros(length(x2),1)]);
HR2 = fft([hrir_R2 ; zeros(length(x2),1)]);

L3 = length(x3) + length(hrir3) -1; % Output data length
X3 = fft([x3 ; zeros(length(hrir_L3),1)]);
HL3 = fft([hrir_L3 ; zeros(length(x3),1)]);
HR3 = fft([hrir_R3 ; zeros(length(x3),1)]);

L4 = length(x4) + length(hrir4) -1; % Output data length
X4 = fft([x4 ; zeros(length(hrir_L4),1)]);
HL4 = fft([hrir_L4 ; zeros(length(x4),1)]);
HR4 = fft([hrir_R4 ; zeros(length(x4),1)]);

L5 = length(x5) + length(hrir5) -1; % Output data length
X5 = fft([x5 ; zeros(length(hrir_L5),1)]);
HL5 = fft([hrir_L5 ; zeros(length(x5),1)]);
HR5 = fft([hrir_R5 ; zeros(length(x5),1)]);

out_L = zeros(L,1); % output buffer
out_R = zeros(L,2);

out_L2 = zeros(L2,1); % output buffer
out_R2 = zeros(L2,2);

out_L3 = zeros(L3,1); % output buffer
out_R3 = zeros(L3,2);

out_L4 = zeros(L4,1); % output buffer
out_R4 = zeros(L4,2);

out_L5 = zeros(L5,1); % output buffer
out_R5 = zeros(L5,2);


out_L = ifft(X .* HL); % multiplication of fft results & ifft
out_R = ifft(X .* HR);

out_L2 = ifft(X2 .* HL2); % multiplication of fft results & ifft
out_R2 = ifft(X2 .* HR2);

out_L3 = ifft(X3 .* HL3); % multiplication of fft results & ifft
out_R3 = ifft(X3 .* HR3);

out_L4 = ifft(X4 .* HL4); % multiplication of fft results & ifft
out_R4 = ifft(X4 .* HR4);

out_L5 = ifft(X5 .* HL5); % multiplication of fft results & ifft
out_R5 = ifft(X5 .* HR5);

out_L = out_L + out_L2 + out_L3 + out_L4 + out_L5;
out_R = out_R + out_R2 + out_R3 + out_R4 + out_R5;

out = [out_L out_R];

sound(out, Fs)

1 个答案:

答案 0 :(得分:0)

您可以尝试使用单元格数组。这些是所有类型数据的通用容器:

fn = {'a.wav','b.wav','c.wav'}

for i=1:length(fn)
  h{i} = wavread(fn{i})

  hrir_L{i} = h{i}(:,1);
  hrir_R{i} = h{i}(:,2);

  ....

end