function return_val = isip_resample(input_file_list, output_file_list, start_rate, end_rate)
%ISIP_RESAMPLE ISIP_RESAMPLE(input_file_list, output_file_list, start_rate, end_rate)

% file: isip_resample.m
%
% example use: isip_resample(input_list, output_list, 20000, 8000)
%

% open the input file
%
f_in_list = fopen(input_file_list, 'rt');
f_out_list = fopen(output_file_list, 'rt');


% loop over files in each list
%
ins = 0;
while (feof(f_in_list) ~= 1) 
     ins = ins + 1;
     in_file = fscanf(f_in_list, '%s', 1);
end;

outs = 0;
while (feof(f_out_list) ~= 1) 
     outs = outs + 1;
     out_file = fscanf(f_out_list, '%s', 1);
end;

% make sure the list sizes line up
%
if ((ins ~= outs) & (ins ~= 0))
    echo "file lists are not the same size" 
    return;
else

    % rewind the input and output list file
    %
    frewind(f_in_list);
    frewind(f_out_list);

    % loop over the files
    %
    num_files = ins - 1

    for file_num = 1:1:num_files
        in_raw_file = fscanf(f_in_list, '%s', 1)
        out_raw_file = fscanf(f_out_list, '%s', 1)
	
        % open the input file in binary read mode with byte-swapping
        %
        f_in = fopen(in_raw_file, 'r', 'ieee-be');
        f_out = fopen(out_raw_file, 'w', 'ieee-be');

	% read the input vector into memory
        %
	in_vector = fread(f_in, 'int16');
        len = length(in_vector) / 2

        % copy the input vector to A & B vector
	%
	in_vec_a=1:len;
        in_vec_b=1:len;
	for k = 1:len
	  in_vec_a(k) = in_vector(2*k-1);
          in_vec_b(k) = in_vector(2*k);
        end;

        clear in_vector;

        % downsample the data
	%
	sprintf('%s', 'downsampling')
	out_vec_a = resample(in_vec_a, end_rate, start_rate, 50);
	out_vec_b = resample(in_vec_b, end_rate, start_rate, 50);

	clear in_vec_a;
	clear in_vec_b;

        % combine two channel to one
	%
	len = length(out_vec_a)
	sprintf('%s %s', 'writing', out_raw_file)
	out_vector=1:(len*2);
        for k = 1:len
	  out_vector(2*k-1) = out_vec_a(k);
          out_vector(2*k) = out_vec_b(k);
        end;

        clear out_vec_a;
        clear out_vec_b;

	% write the data out
	%
	fwrite(f_out, out_vector, 'int16');

        clear out_vector;

        % close the input and output files
	%
	fclose(f_in);
	fclose(f_out);
   end;
end;
