Matlab Help Help Help


Diamondme
09-08-2008, 03:32 PM
I have been trying to write a matlab program (taking the convolution of two functions) however, the two functions do not have the same number of data points and the sampling rate is not consistant .... Below is my program, which still gives error messages... CAN ANYBODY HELP ME????:eek:


function FilterConv

fid=0; % Open data file
fprintf(1,'\n')
while fid < 1
filename='alexa.txt';
[fid,message] = fopen(filename, 'r');
if fid == -1
disp(message)
end
end
[Spect, count] = fscanf(fid, '%f %f\n',[2, inf]);
fclose(fid);
NSpect = count/2;
for i=1:NSpect
Spect_x(i) = Spect(1,i);
Spect_y(i) = Spect(2,i);
end

fid=0;
fprintf(1,'\n');
while fid < 1
filename='lowpass data.txt';
[fid, message] = fopen(filename, 'r');
if fid == -1
disp(message)
end
end
[Filt, count] = fscanf(fid, '%f %f\n', [2, inf]);
fclose(fid);
NFilt = count/2;
for i=1:NFilt
Filt_x(i) = Filt(1,i);
Filt_y(i) = Filt(2,i);
end


%Lambda_min = max(Spect_x(1), Filt_x(1));
Lambda_min = input('Lambda_min = ');
%Lambda_max = min(Spect_x(NSpect), Filt_x(NFilt));
Lambda_max = input('Lambda_max = ');
Ndp = input('Number of data points: ');
inc = (Lambda_max - Lambda_min)/(Ndp - 1);
Xi = Lambda_min:inc:Lambda_max;


Spect_yi = interp1q(Spect_x.', Spect_y.', Xi.');

Filt_yi = interp1q(Filt_x.', Filt_y.', Xi.');


if Lambda_min < Spect_x(1)
Spect_pad_low = Lambda_min:inc:Spect_x(1)-inc;
elseif Lambda_max > Spect_x(Nspect)
Spect_pad_high = Spect_x(Nspect)+inc:inc:Lambda_max;
else
Spect_pad_low = [];
Spect_pad_high = [];
end
Xi = [Spect_pad_low Xi Spect_pad_high];


if Lambda_min < Filt_y(1)
Filt_pad_low = Lambda_min:inc:Filt_y(1)-inc;
elseif Lambda_max > Filt_y(Nfilt)
Filt_pad_high = Filt_y(Nfilt)+inc:inc:Lambda_max;
else
Filt_pad_low = []
Filt_pad_high = []
end
Yi = [Filt_pad_low Yi Filt_pad_high]



Conv_inc = (Lambda_max - Lambda_min)/(2*Ndp - 2);
Conv_i = conv(Spect_yi, Filt_yi);
Xc_i = Lambda_min:Conv_inc:Lambda_max;



filename = 'outputResult.txt';
[fid] = fopen(filename, 'w');
for i=1:2*Ndp-2
fprintf(fid,'%f %f\n',Xc_i(i),Conv_i(i));
end
fclose(fid);


subplot(3,1,1);
plot(Xi,Spect_yi);
subplot(3,1,2);
plot(Xi,Filt_yi);
subplot(3,1,3);
plot(Xc_i,Conv_i);

end