%EE683 Group Project
%Ting-Hsiang Chuang

clc
clear all
close all

fid = fopen('fourfireflies.txt');  %load the data file

i=1;                                %initialize variable i

while 1
    
   curlin=fgetl(fid);

   if curlin(1) ~= 'T'              %if the first character is not T
       
      if ~ischar(curlin)            %if it is not a character 
       
          break                     %stop reading
          
      end
            
    T(i,:)=str2num(curlin(1:4));    %store the time in 3 decimal second
    A(i,:)=str2num(curlin(20:27));  %store the first firefly data
    B(i,:)=str2num(curlin(39:46));  %store the second firefly data
    C(i,:)=str2num(curlin(58:65));  %store the third firefly data
    D(i,:)=str2num(curlin(77:84));  %store the third firefly data
  
    if A(i,:)>=0.65
        A(i,:)=1;
    else
        A(i,:)=0;
    end
   
    if B(i,:)>=0.65
        B(i,:)=1;
    else
        B(i,:)=0;
    end
    
    if C(i,:)>=0.65
        C(i,:)=1;
    else
        C(i,:)=0;
    end
    
    if D(i,:)>=0.65
        D(i,:)=1;
    else
        D(i,:)=0;
    end
    
    
    i=i+1;                 
    
   end

end

fclose(fid)

ida=find(A(:)==1);                  %Find the location of 1
idb=find(B(:)==1);
idc=find(C(:)==1);
idd=find(D(:)==1);

ta=T(ida);                          %At the time where A=1 (on)
tb=T(idb);
tc=T(idc);
td=T(idd);

x=[0:0.01:10]';                     %New time axis

ya=zeros(length(0:0.01:10),1);      %Initialize each y-axis
yb=zeros(length(0:0.01:10),1);
yc=zeros(length(0:0.01:10),1);
yd=zeros(length(0:0.01:10),1)

for i=1:length(ya)
    
    for j=1:length(ta)
        if x(i)==ta(j)              %Put 1 in y-axis when the on_time = new time axis
           ya(i)=1;
        end
    end
    
    for j=1:length(tb)
        if x(i)==tb(j)
            yb(i)=1;
        end
    end
    
    for j=1:length(tc)
        if x(i)==tc(j)
            yc(i)=1;
        end
    end
    
    for j=1:length(td)
        if x(i)==td(j)
            yd(i)=1;
        end
    end
    
end

%Make light bulb 
n = -180:180;
t = sin(n*pi/180);
v = cos(n*pi/180);

% Define the area to be recorded
rect = get(gcf,'Position');
rect(1:2) = [0 0];

m=1;

for xx=1:length(ya)
   
    tt(m,:)=x(xx,:);                %Change value of xx can vary the sampling time 
    yya(m,:)=ya(xx,:);           
    yyb(m,:)=yb(xx,:);
    yyc(m,:)=yc(xx,:);
    yyd(m,:)=yd(xx,:);
    
    subplot(221)
    if yya(m)==1    
       fill(t,v,'g');               %Fill the green color in the light bulb 
    else                            %when y-axis = 1 
       fill(t,v,'w');
    end
    axis off

    subplot(222)
    if yyb(m)==1
       fill(t,v,'g');
    else
       fill(t,v,'w');
    end
    axis off

    subplot(223)
    if yyc(m)==1
       fill(t,v,'g');
    else
       fill(t,v,'w');
    end
    axis off
    
    subplot(224)
    if yyd(m)==1
       fill(t,v,'g');
    else
       fill(t,v,'w');
    end
    axis off

    M(m)=getframe(gcf,rect);

    m=m+1;
    
end


%convert to MPEG format

map=colormap;

mpgwrite(M, map,'movie3')