%%
clear all;close all

Cell1=[0	1.5	3.1	4.8	7	9.6	12.15	13.3	15.1	17	19	21	22
0.19	0.46	0.75	1.07	1.49	1.96	2.44	2.67	3	3.37	3.74	4.11	4.3
30	86	141	207	292	387	482	529	596	669	744	818	856
38	92	148	213	299	393	489	535	602	675	751	825	863											
36	91	147	211	297	391	487	533	600	673	748	822	860
];

Cell2=[0	1.4	3	4.6	6.8	9	11.5	13	14.8	16.5	18	20.1	22.1
0.86	1.1	1.39	1.6	2.12	2.55	3.03	3.3	3.67	3.97	4.26	4.64	5.03
164	214	271	335	419	504	600	658	728	790	847	924	1004
171	220	278	341	425	510	607	664	735	796	853	931	1010											
169	219	276	339	423	508	604	662	732	793	851	928	1008
];

Cell3=[ 0	1.4	3.2	4.5	6.7	9	11.5	13.1	14.8	16.4	18	20	22
0.34	0.6	0.92	1.16	1.57	1.99	2.45	2.74	3.07	3.36	3.64	3.96	4.31
60	113	177	225	308	391	485	543	609	667	723	800	873
67	120	183	231	314	398	491	549	615	673	729	807	880											
66	119	182	230	313	396	489	547	614	671	727	792	865
];

Cell4=[0	1.5	3	4.5	6.5	9	11.5	13.1	14.75	16.4	18	20	22
0.63	0.95	1.22	1.5	1.86	2.34	2.77	3.05	3.35	3.6	3.91	4.25	4.61
119	183	237	295	368	466	556	618	680	736	800	873	948
126	189	243	300	374	472	562	624	687	743	806	880	954
												
124	187	240	298	369	465	551	605	668	720	778	850	925
];

Cell5=[0	3.2	7.2	10	13.5	17	20.5	24	27.5	31	34.5	40	44
0.3	0.26	0.59	0.82	1.1	1.39	1.66	1.94	2.21	2.52	2.8	3.26	3.6
1	50	115	161	217	270	326	382	443	504	558	649	713
6	57	121	167	223	276	332	386	447	510	563	655	718
4	55	118	164	221	273	329	383	444	507	561	652	715
];

Cell6=[0	3.3	7.2	10	13.7	17.3	20.2	25.1	28.35	32	35	39.3	44
0.13	0.37	0.7	0.94	1.23	1.53	1.77	2.2	2.48	2.78	3.04	3.4	3.8
19	68	134	180	242	300	350	436	491	551	604	676	756
25	75	141	186	248	305	356	442	497	558	610	682	763									
24	73	139	185	246	304	354	440	496	556	608	680	760
];

Cell7=[0	3.5	7.35	10.3	14.15	17	20	24	28.45	31.9	35	39.75	43.9
0.15	0.43	0.74	0.97	1.28	1.51	1.76	2.08	2.44	2.71	2.98	3.36	3.7
23	78	140	188	250	297	345	409	483	538	590	668	736
29	84	147	195	256	303	352	416	489	544	596	674	742												
28	83	145	193	255	301	350	414	487	542	594	672	740
];

Cell8=[0	3.8	7.8	11.3	15.3	18.5	21.3	25.6	29.3	33	36.55	40.5	44.2
0.1	0.38	0.7	0.97	1.29	1.54	1.78	2.12	2.4	2.72	2.99	3.3	3.61
14	72	132	188	252	306	348	420	479	536	592	658	716
20	79	137	193	258	312	354	423	486	540	596	665	722											
18	78	136	192	256	311	352	422	483	539	595	663	719
];

Cells(:,:,1)=Cell1;
Cells(:,:,2)=Cell2;
Cells(:,:,3)=Cell3;
Cells(:,:,4)=Cell4;
Cells(:,:,5)=Cell5;
Cells(:,:,6)=Cell6;
Cells(:,:,7)=Cell7;
Cells(:,:,8)=Cell8;

for n=1:8
%     figure('position',[1000 50 900 900],'color',[1 1 1])
%     set(gca, 'LooseInset',[0.2 0.05 0.05 0.05])
%     set(gca, 'LooseInset', get(gca,'TightInset'))
%     
%     rpm=linspace(0,8000,100);
%     alpha = (rpm-6000)*(1/(8000-6000));
%     alpha(alpha<0)=0;
%     
%     plot(rpm,alpha);
%     set(gca,'box','off')
%     grid on


    h1=figure;
    set(h1,'Name',sprintf('cell%dvolt',n),'Color',[1,1,1], 'Position' ,[100 100 840 630],'PaperPositionMode','auto','PaperUnits','centimeters',...
        'PaperSize',[23 19])
    plot(Cells(1,:,n),Cells(2,:,n),'b+','MarkerSize',15,'LineWidth',5)
%     title(sprintf('Cell %d Tension',n))
    
    Poly_V(n,:)=polyfit(Cells(2,:,n),Cells(1,:,n),1);
    
     R2_V(n)=Rsquare(Cells(2,:,n),Cells(1,:,n),Poly_V(n,:));
    
    grid on
%     set(gca, 'LooseInset',[0.08 0.08 0.08 0.08])
    set(gca,'box','off','FontSize',25,'YLim',[0 5])
    
     xlabel('Force (N)')
     ylabel('Voltage (V)','Units', 'Normalized', 'Position', [-0.05 0.5])
    
    print( h1, '-dpdf','-r1200', sprintf('cell%dvolt.pdf',n))

    h2=figure;
     set(h2,'Name',sprintf('cell%dadc',n),'Color',[1,1,1], 'Position' ,[100 100 840 630],'PaperPositionMode','auto','PaperUnits','centimeters',...
        'PaperSize',[23 19])
    
    ADC(n,:)=mean(Cells(3:5,:,n),1 );
    
    plot(Cells(1,:,n),ADC(n,:),'r+','MarkerSize',15,'LineWidth',5)
%     title(sprintf('Cell %d ADC',n))
    
    Poly_ADC(n,:)=polyfit(ADC(n,:),Cells(1,:,n),1);
    Poly_ADC_(n,:)=polyfit(Cells(1,:,n),ADC(n,:),1);
    
    R2_ADC(n)=Rsquare(ADC(n,:), Cells(1,:,n),Poly_ADC(n,:));
    
    
    
    grid on
%     set(gca, 'LooseInset',[0.08 0.08 0.08 0.08])
    set(gca,'box','off','FontSize',25,'YLim',[0 1024],'YTickMode','manual','YTickLabel',[0;200;400;600;800;1023],'YTick',[0;200;400;600;800;1023])

    xlabel('Force (N)')
    ylabel('ADC value','Units', 'Normalized', 'Position', [-0.085 0.5])
    
    print( h2, '-dpdf','-r1200', sprintf('cell%dadc.pdf',n))
    
%     get(h1)
%     get(h2)
end

%%

y=Poly_ADC_(1,1).*Cells(1,:,1)+Poly_ADC_(1,2)
dif=[Cells(3,:,1)-y;Cells(4,:,1)-y;Cells(5,:,1)-y]
dif=abs(dif)
s_dif=sum(dif,1)
m3_dif=s_dif./3


