%Read in the data from the file
%A=0;

fid = fopen('signal_swapped.ra');
[A,count] = fread(fid,inf,'short');
fclose(fid);

%Filter the original data using three different filters.
F1=1:count;
F1(1)=0;
for i=2:count;
   F1(i)=(1/2)*A(i-1)+(1/2)*A(i);
end

F2(1)=0;
F2(count)=0;
for i=2:count-1;                     
  F2(i)=(1/3)*A(i-1)+(1/3)*A(i)+(1/3)*A(i+1);
end



F3=1:count;
F3(1)=0;
for i=2:count;
   F3(i)=0.5*F3(i-1)+A(i)*A(i);
end


%Write filtered data to files

fnew = 0;
fnew=fopen('F1_filter_swapped.raw','w')

fwrite(fnew,F1,'short');
                                                  
fclose(fnew);


fnew = 0;

fnew=fopen('F2_filter_swapped.raw','w')

fwrite(fnew,F2,'short');

fclose(fnew);


fnew = 0;

fnew=fopen('F3_filter_swapped.raw','w')

fwrite(fnew,F3,'short');          

fclose(fnew);



%Plot the filtered waveforms
figure (1);
A=0;
fid = 0;
fid = fopen('F1_filter_swapped.raw');
[A,count] = fread(fid,inf,'short');
fclose(fid);

plot(A),axis ([1 count -3e4 3e4]);

figure (2);
B=0;
fid = fopen('F2_filter_swapped.raw');
[B,count] = fread(fid,inf,'short');
fclose(fid);


plot(B),axis ([1 count -3e4 3e4]);

figure (3);
C=0;
fid = fopen('F3_filter_swapped.raw');
[C,count] = fread(fid,inf,'short');
fclose(fid);

plot(F3),axis ([1 count -3e4 3e4]);                
