Change input video from saved video to streaming video for manipulation - matlab

I downloaded this code from MIT's video magnification lab: http://people.csail.mit.edu/mrub/evm/#code
Except it currently only runs on saved videos. We wanted to change it so that we can have a live video set up.
The matlab code is as follows:
dataDir = './data';
resultsDir = 'ResultsSIGGRAPH2012';
mkdir(resultsDir);
inFile = fullfile(dataDir,'face2.mp4');
fprintf('Processing %s\n', inFile);
% Motion
amplify_spatial_lpyr_temporal_butter(inFile,resultsDir,20,80, ...
0.5,10,30, 0);
The amplify_spatial_lpyr_temporal_butter matlab code is:
function amplify_spatial_lpyr_temporal_butter(vidFile, outDir ...
,alpha, lambda_c, fl, fh ...
,samplingRate, chromAttenuation)
[low_a, low_b] = butter(1, fl/samplingRate, 'low');
[high_a, high_b] = butter(1, fh/samplingRate, 'low');
[~,vidName] = fileparts(vidFile);
outName = fullfile(outDir,[vidName '-butter-from-' num2str(fl) '-to-' ...
num2str(fh) '-alpha-' num2str(alpha) '-lambda_c-' num2str(lambda_c) ...
'-chromAtn-' num2str(chromAttenuation) '.avi']);
% Read video
vid = VideoReader(vidFile);
% Extract video info
vidHeight = vid.Height;
vidWidth = vid.Width;
nChannels = 3;
fr = vid.FrameRate;
len = vid.NumberOfFrames;
temp = struct('cdata', ...
zeros(vidHeight, vidWidth, nChannels, 'uint8'), ...
'colormap', []);
startIndex = 1;
endIndex = len-10;
vidOut = VideoWriter(outName);
vidOut.FrameRate = fr;
open(vidOut)
% firstFrame
temp.cdata = read(vid, startIndex);
[rgbframe,~] = frame2im(temp);
rgbframe = im2double(rgbframe);
frame = rgb2ntsc(rgbframe);
[pyr,pind] = buildLpyr(frame(:,:,1),'auto');
pyr = repmat(pyr,[1 3]);
[pyr(:,2),~] = buildLpyr(frame(:,:,2),'auto');
[pyr(:,3),~] = buildLpyr(frame(:,:,3),'auto');
lowpass1 = pyr;
lowpass2 = pyr;
pyr_prev = pyr;
output = rgbframe;
writeVideo(vidOut,im2uint8(output));
nLevels = size(pind,1);
for i=startIndex+1:endIndex
progmeter(i-startIndex,endIndex - startIndex + 1);
temp.cdata = read(vid, i);
[rgbframe,~] = frame2im(temp);
rgbframe = im2double(rgbframe);
frame = rgb2ntsc(rgbframe);
[pyr(:,1),~] = buildLpyr(frame(:,:,1),'auto');
[pyr(:,2),~] = buildLpyr(frame(:,:,2),'auto');
[pyr(:,3),~] = buildLpyr(frame(:,:,3),'auto');
%% temporal filtering
lowpass1 = (-high_b(2) .* lowpass1 + high_a(1).*pyr + ...
high_a(2).*pyr_prev)./high_b(1);
lowpass2 = (-low_b(2) .* lowpass2 + low_a(1).*pyr + ...
low_a(2).*pyr_prev)./low_b(1);
filtered = (lowpass1 - lowpass2);
pyr_prev = pyr;
%% amplify each spatial frequency bands according to Figure 6 of our paper
ind = size(pyr,1);
delta = lambda_c/8/(1+alpha);
% the factor to boost alpha above the bound we have in the
% paper. (for better visualization)
exaggeration_factor = 2;
% compute the representative wavelength lambda for the lowest spatial
% freqency band of Laplacian pyramid
lambda = (vidHeight^2 + vidWidth^2).^0.5/3; % 3 is experimental constant
for l = nLevels:-1:1
indices = ind-prod(pind(l,:))+1:ind;
% compute modified alpha for this level
currAlpha = lambda/delta/8 - 1;
currAlpha = currAlpha*exaggeration_factor;
if (l == nLevels || l == 1) % ignore the highest and lowest frequency band
filtered(indices,:) = 0;
elseif (currAlpha > alpha) % representative lambda exceeds lambda_c
filtered(indices,:) = alpha*filtered(indices,:);
else
filtered(indices,:) = currAlpha*filtered(indices,:);
end
ind = ind - prod(pind(l,:));
% go one level down on pyramid,
% representative lambda will reduce by factor of 2
lambda = lambda/2;
end
%% Render on the input video
output = zeros(size(frame));
output(:,:,1) = reconLpyr(filtered(:,1),pind);
output(:,:,2) = reconLpyr(filtered(:,2),pind);
output(:,:,3) = reconLpyr(filtered(:,3),pind);
output(:,:,2) = output(:,:,2)*chromAttenuation;
output(:,:,3) = output(:,:,3)*chromAttenuation;
output = frame + output;
output = ntsc2rgb(output);
% filtered = rgbframe + filtered.*mask;
output(output > 1) = 1;
output(output < 0) = 0;
writeVideo(vidOut,im2uint8(output));
end
close(vidOut);
end
We are trying to get it to work with a streaming video input, but don't really know where to start. We are somewhat familiar with programming but not so familiar with Matlab and so don't really know where to look for such answers.
Any help would be greatly appreciated.

Related

Problems with initializing CamShift algorithm

I use matlab to implement a tracker using cam shift. I need to implement it myself hence I'm not using the built in method.
I let the user choose the object to track and after the first iteration the convergence makes it start tracking a different object. pictures speaks louder than words so:
I choose my face in the first frame:
It converges to my shoulder and starting tracking it:
Now the tracking is overall ok throughout the video so I think the issue is in the initialization phase.
SomeCode: Get position from user:
function [pos] = getObjectPosition(F)
f = figure;
imshow(F);
pos = getPosition(imrect(gca));
close(f);
end
Main Loop:
firstFrame = readFrame(input);
pos = getObjectPosition(firstFrame);
while hasFrame(input)
% init next frame
nextFrame = readFrame(input);
[currFrameHSV, ~, ~] = rgb2hsv(nextFrame);
frameHue = double(currFrameHSV(:,:,1));
% init while loop vars
prevX = -1;
prevY = -1;
i = 0;
while (i < numOfIterations) && ~(prevX == pos(1) && prevY == pos(2))
% updating the curr iteration number
i = i + 1;
% getting search window location and dimensions
envRect = round(getEnvRect(pos,pos(4)/yRatio,pos(3)/xRatio,height,width));
[X,Y] = meshgrid(envRect(1):envRect(1) + envRect(3),...
envRect(2):envRect(2) + envRect(4));
% getting the sub image
I = imcrop(frameHue,envRect);
M00 = sum(sum(I));
M10 = sum(sum(X.*I));
M01 = sum(sum(Y.*I));
% getting center mass location
Xc = round(M10/M00);
Yc = round(M01/M00);
% updating object position
prevX = pos(1);
prevY = pos(2);
pos(1) = floor(Xc - pos(3)/2);
pos(2) = floor(Yc - pos(4)/2);
end
% adding the object's bounding box to the frame to write
% show the image
end
getEnv method:
function [ envRect ] = getEnvRect( feature,hShift,wShift,rows,cols )
hEnvSize = feature(4) + 2*hShift;
wEnvSize = feature(3) + 2*wShift;
xPatch = feature(1);
xEnv = max(1, xPatch - wShift);
yPatch = feature(2);
yEnv = max(1, yPatch - hShift);
width = min(cols, xEnv + wEnvSize) - xEnv;
height = min(rows, yEnv + hEnvSize) - yEnv;
envRect = [xEnv, yEnv, width, height];
end

Matlab Neutron image reconstructions

I am trying to reconstruct an image using the projections from the Neutron image scanner. I am using the following code. I am not able to obtain a meaningful reconstructed image.
Can anybody advise me on where I am going wrong.
much appreciated,
Vani
filename = strcat(' Z:\NIST_Data\2016\SEPT\example reconstructed\carboxylic\carboxylic reconstructed part 3\Coral\',srcFiles(i).name);
I=imread(filename);
P = im2double(I);
if i == 1
array3d = P;
else
array3d = cat(3, array3d, P);
end
end
num = size(array3d,3);
for p = 1:num
PR = double(squeeze(array3d(p,:,:)));
[L,C]=size(PR);
w = [-pi : (2*pi)/L : pi-(2*pi)/L];
Filt = abs(sin(w));
Filt = Filt(1:463);
for i = 1:C,
IMG = fft(PR(:,i));
end
FiltIMG = IMG*Filt; %FiltIMG = filter (b, a, IMG);
% Remove any remaining imaginary parts
FIL = real(FiltIMG);
% filter the projections
%filtPR = projfilter(PR);
%filtPR = filterplus(PR);
filtPR = FIL;
THETA=0:180;
% figure out how big our picture is going to be.
n = size(filtPR,1);
sideSize = n;
% convert THETA to radians
th = (pi/180)*THETA;
% set up the image
m = length(THETA);
BPI = zeros(sideSize,sideSize);
% find the middle index of the projections
midindex = (n+1)/2;
% set up x and y matrices
x = 1:sideSize;
y = 1:sideSize;
[X,Y] = meshgrid(x,y);
xpr = X - (sideSize+1)/2;
ypr = Y - (sideSize+1)/2;
% loop over each projection
%figure
%colormap(jet)
%M = moviein(m);
for i = 1:m
tic
disp(['On angle ', num2str(THETA(i))]);
% figure out which projections to add to which spots
filtIndex = round(midindex + xpr*sin(th(i)) - ypr*cos(th(i)));
% if we are "in bounds" then add the point
BPIa = zeros(sideSize,sideSize);
spota = find((filtIndex > 0) & (filtIndex <= n));
newfiltIndex = filtIndex(spota);
BPIa(spota) = filtPR(newfiltIndex(:),i);
%keyboard
BPI = BPI + BPIa;
toc
%imagesc(BPI)
%M(:,i) = getframe;
%figure(2)
%plot(filtPR(:,i));
%keyboard
end
BPI = BPI./m;
h=figure
imagesc(BPI)
saveas(h,sprintf('filtsli-FIG%d.tif',p));end

Efficient solution to object detection algorithm

I'm trying to implement this paper 'Salient Object detection by composition' here is the link: http://research.microsoft.com/en-us/people/yichenw/iccv11_salientobjectdetection.pdf
I have implemented the algorithm but it takes a long time to execute and display the output. I'm using 4 for loops in the code(Using for loops is the only way I could think of to implement this algorithm.) I have searched online for MATLAB code, but couldn't find anything. So can anyone please suggest any faster way to implement the algorithm. Also in the paper they(the authors) say that they have implemented the code using MATLAB and it runs quickly. So there definitely is a way to write the code more efficiently.
I appreciate any hint or code to execute this algorithm efficiently.
clc
clear all
close all
%%instructions to run segment.cpp
%to run this code
%we need an output image
%segment sigma K min input output
%sigma: used for gaussian smoothing of the image
%K: scale of observation; larger K means larger components in segmentation
%min: minimum component size enforced by post processing
%%
%calculating composition cost for each segment
I_org = imread('segment\1.ppm');
I = imread('segment\output1.ppm');
[rows,cols,dims] = size(I);
pixels = zeros(rows*cols,dims);
red_channel = I(:,:,1);
green_channel = I(:,:,2);
blue_channel = I(:,:,3);
[unique_pixels,count_pixels] = countPixels(I);
no_segments = size(count_pixels,1);
area_segments = count_pixels ./ (rows * cols);
appearance_distance = zeros(no_segments,no_segments);
spatial_distance = zeros(no_segments,no_segments);
thresh = multithresh(I_org,11);
thresh_values = [0 thresh];
for i = 1:no_segments
leave_pixel = unique_pixels(i,:);
mask_image = ((I(:,:,1) == leave_pixel(1)) & (I(:,:,2) == leave_pixel(2)) & (I(:,:,3) == leave_pixel(3)));
I_i(:,:,1) = I_org(:,:,1) .* uint8((mask_image));
I_i(:,:,2) = I_org(:,:,2) .* uint8((mask_image));
I_i(:,:,3) = I_org(:,:,3) .* uint8((mask_image));
LAB_trans = makecform('srgb2lab');
I_i_LAB = applycform(I_i,LAB_trans);
L_i_LAB = imhist(I_i_LAB(:,:,1));
A_i_LAB = imhist(I_i_LAB(:,:,2));
B_i_LAB = imhist(I_i_LAB(:,:,3));
for j = i:no_segments
leave_pixel = unique_pixels(j,:);
mask_image = ((I(:,:,1) == leave_pixel(1)) & (I(:,:,2) == leave_pixel(2)) & (I(:,:,3) == leave_pixel(3)));
I_j(:,:,1) = I_org(:,:,1) .* uint8((mask_image));
I_j(:,:,2) = I_org(:,:,2) .* uint8((mask_image));
I_j(:,:,3) = I_org(:,:,3) .* uint8((mask_image));
I_j_LAB = applycform(I_j,LAB_trans);
L_j_LAB = imhist(I_j_LAB(:,:,1));
A_j_LAB = imhist(I_j_LAB(:,:,2));
B_j_LAB = imhist(I_j_LAB(:,:,3));
appearance_distance(i,j) = sum(min(L_i_LAB,L_j_LAB) + min(A_i_LAB,A_j_LAB) + min(B_i_LAB,B_j_LAB));
spatial_distance(i,j) = ModHausdorffDist(I_i,I_j) / max(rows,cols);
end
end
spatial_distance = spatial_distance ./ max(max(spatial_distance));
max_apperance_distance = max(max(appearance_distance));
composition_cost = ((1 - spatial_distance) .* appearance_distance) + (spatial_distance * max_apperance_distance);
%%
%input parameters for computation
window_size = 9; %rows and colums are considered to be same
window = ones(window_size);
additional_elements = (window_size - 1)/2;
I_temp(:,:,1) = [zeros(additional_elements,cols);I(:,:,1);zeros(additional_elements,cols)];
I_new(:,:,1) = [zeros(rows + (window_size - 1),additional_elements) I_temp(:,:,1) zeros(rows + (window_size - 1),additional_elements)];
I_temp(:,:,2) = [zeros(additional_elements,cols);I(:,:,2);zeros(additional_elements,cols)];
I_new(:,:,2) = [zeros(rows + (window_size - 1),additional_elements) I_temp(:,:,2) zeros(rows + (window_size - 1),additional_elements)];
I_temp(:,:,3) = [zeros(additional_elements,cols);I(:,:,3);zeros(additional_elements,cols)];
I_new(:,:,3) = [zeros(rows + (window_size - 1),additional_elements) I_temp(:,:,3) zeros(rows + (window_size - 1),additional_elements)];
cost = zeros(rows,cols);
for i = additional_elements + 1:rows
for j = additional_elements+1:cols
I_windowed(:,:,1) = I_new(i-additional_elements:i+additional_elements,i-additional_elements:i+additional_elements,1);
I_windowed(:,:,2) = I_new(i-additional_elements:i+additional_elements,i-additional_elements:i+additional_elements,2);
I_windowed(:,:,3) = I_new(i-additional_elements:i+additional_elements,i-additional_elements:i+additional_elements,3);
[unique_pixels_w,count_pixels_w] = countPixels(I_windowed);
unique_pixels_w = setdiff(unique_pixels_w,[0 0 0],'rows');
inside_segment = setdiff(unique_pixels,unique_pixels_w);
outside_segments = setdiff(unique_pixels,inside_segment);
area_segment = count_pixels_w;
for k = 1:size(inside_pixels,1)
current_segment = inside_segment(k,:);
cost_curr_seg = sort(composition_cost(ismember(unique_pixels,current_segment,'rows'),:));
for l = 1:size(cost_curr_seg,2)
if(ismember(unique_pixels(l,:),outside_segments,'rows') && count_pixels(l) > 0)
composed_area = min(area_segment(k),count_pixels(l));
cost(i,j) = cost(i,j) + cost_curr_seg(l) * composed_area;
area_segment(k) = area_segment(k) - composed_area;
count_pixels(l) = count_pixels(l) - composed_area;
if area_segment(k) == 0
break
end
end
end
if area(k) > 0
cost(i,j) = cost(i,j) + max_apperance_distance * area_segment(k);
end
end
end
end
cost = cost / window_size;
The code for the countPixels function:
function [unique_rows,counts] = countPixels(I)
[rows,cols,dims] = size(I);
pixels_I = zeros(rows*cols,dims);
count = 1;
for i = 1:rows
for j = 1:cols
pixels_I(count,:) = reshape(I(i,j,:),[1,3]);
count = count + 1;
end
end
[unique_rows,~,ind] = unique(pixels_I,'rows');
counts = histc(ind,unique(ind));
end

Index exceeds matrix dimensions error in Runge-Kutta method: Matlab

I'm trying to make a time stepping code using the 4th order Runge-Kutta method but am running into issues indexing one of my values properly. My code is:
clc;
clear all;
L = 32; M = 32; N = 32; % No. of elements
Lx = 2; Ly = 2; Lz = 2; % Size of each element
dx = Lx/L; dy = Ly/M; dz = Lz/N; % Step size
Tt = 1;
t0 = 0; % Initial condition
T = 50; % Final time
dt = (Tt-t0)/T; % Determining time step interval
% Wave characteristics
H = 2; % Wave height
a = H/2; % Amplitude
Te = 6; % Period
omega = 2*pi/Te; % Wave rotational frequency
d = 25; % Water depth
x = 0; % Location of cylinder axis
u0(1:L,1:M,1:N,1) = 0; % Setting up solution space matrix (u values)
v0(1:L,1:M,1:N,1) = 0; % Setting up solution space matrix (v values)
w0(1:L,1:M,1:N,1) = 0; % Setting up solution space matrix (w values)
[k,L] = disp(d,omega); % Solving for k and wavelength using Newton-Raphson function
%u = zeros(1,50);
%v = zeros(1,50);
%w = zeros(1,50);
time = 1:1:50;
for t = 1:T
for i = 1:L
for j = 1:M
for k = 1:N
eta(i,j,k,t) = a*cos(omega*time(1,t);
u(i,j,k,1) = u0(i,j,k,1);
v(i,j,k,1) = v0(i,j,k,1);
w(i,j,k,1) = w0(i,j,k,1);
umag(i,j,k,t) = a*omega*(cosh(k*(d+eta(i,j,k,t))))/sinh(k*d);
vmag(i,j,k,t) = 0;
wmag(i,j,k,t) = -a*omega*(sinh(k*(d+eta(i,j,k,t))))/sinh(k*d);
uRHS(i,j,k,t) = umag(i,j,k,t)*cos(k*x-omega*t);
vRHS(i,j,k,t) = vmag(i,j,k,t)*sin(k*x-omega*t);
wRHS(i,j,k,t) = wmag(i,j,k,t)*sin(k*x-omega*t);
k1x(i,j,k,t) = dt*uRHS(i,j,k,t);
k2x(i,j,k,t) = dt*(0.5*k1x(i,j,k,t) + dt*uRHS(i,j,k,t));
k3x(i,j,k,t) = dt*(0.5*k2x(i,j,k,t) + dt*uRHS(i,j,k,t));
k4x(i,j,k,t) = dt*(k3x(i,j,k,t) + dt*uRHS(i,j,k,t));
u(i,j,k,t+1) = u(i,j,k,t) + (1/6)*(k1x(i,j,k,t) + 2*k2x(i,j,k,t) + 2*k3x(i,j,k,t) + k4x(i,j,k,t));
k1y(i,j,k,t) = dt*vRHS(i,j,k,t);
k2y(i,j,k,t) = dt*(0.5*k1y(i,j,k,t) + dt*vRHS(i,j,k,t));
k3y(i,j,k,t) = dt*(0.5*k2y(i,j,k,t) + dt*vRHS(i,j,k,t));
k4y(i,j,k,t) = dt*(k3y(i,j,k,t) + dt*vRHS(i,j,k,t));
v(i,j,k,t+1) = v(i,j,k,t) + (1/6)*(k1y(i,j,k,t) + 2*k2y(i,j,k,t) + 2*k3y(i,j,k,t) + k4y(i,j,k,t));
k1z(i,j,k,t) = dt*wRHS(i,j,k,t);
k2z(i,j,k,t) = dt*(0.5*k1z(i,j,k,t) + dt*wRHS(i,j,k,t));
k3z(i,j,k,t) = dt*(0.5*k2z(i,j,k,t) + dt*wRHS(i,j,k,t));
k4z(i,j,k,t) = dt*(k3z(i,j,k,t) + dt*wRHS(i,j,k,t));
w(i,j,k,t+1) = w(i,j,k,t) + (1/6)*(k1z(i,j,k,t) + 2*k2z(i,j,k,t) + 2*k3z(i,j,k,t) + k4z(i,j,k,t));
a(i,j,k,t+1) = ((u(i,j,k,t+1))^2 + (v(i,j,k,t+1))^2 + (w(i,j,k,t+1))^2)^0.5;
end
end
end
end
At the moment, the values seem to be fine for the first iteration but then I have the error Index exceeds matrix dimension in the line calculating eta. I understand that I am not correctly indexing the eta value but am not sure how to correct this.
My goal is to update the value of eta for each loop of t and then use that new eta value for the rest of the calculations.
I'm still quite new to programming and am trying to understand indexing, especially in 3 or 4 dimensional matrices and would really appreciate any advice in correctly calculating this value.
Thanks in advance for any advice!
You declare
time = 1:1:50;
which is just a row vector but access it here
eta(i,j,k,t) = a*cos(omega*time(i,j,k,t));
as if it were an array with 4 dimensions.
To correctly access element x of time you need to use syntax
time(1,x);
(as it is a 1 x 50 array)

MATLAB simplified loop

Okay, so i'm struggling with this one. The code below works to how i need it to, however, i'm sure it could be shorter and simpler. I am aware that i could possibly use a for loop in order to make it shorter i just don't know how, could anyone point me in the right direction in doing this. Thanks :)
if it's not clear form the code what i'm trying to do. I'm trying to convolve separate signals and room impulse responses together using a different process other than the convolution function
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)
You could try using cell arrays. These are general containers for all types of data:
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