Commit db137227 authored by ncarmignani's avatar ncarmignani
Browse files

Code optimization done by Nicola and Andrea

parent daf35519
function [normI,phi,firstX,MresH,MresVH] = CalMresfdaH(n_and_dateX,Xamp,qemres)
%uses calculated sine and cosine instead of fda Cor96XZstuf signals with a shift by 22 samples for the Q Cor signal
%uses calculated sine and cosine instead of fda CorNSTEXZstuf signals with a shift by 22 samples for the Q Cor signal
% to get the Iand Q data
%set_path
clockstart=clock
% set_path % TO BE COMMENTED OUT WHEN MERGING WITH QEMPANEL
tic
%load lastMresV
[Xsnif, Zsnif] =getFDAreaderXZ(n_and_dateX);
[XCor, ZCor] =getFDAreaderXZCor(n_and_dateX);
siz=size(Xsnif)
MresH=zeros(224,96);
MresV=zeros(224,96);
MresVH=zeros(224,96);
MresHI=zeros(224,96);
MresVI=zeros(224,96);
MresHQ=zeros(224,96);
MresVQ=zeros(224,96);
MresHVQ=zeros(224,96);
siz=size(Xsnif);
NBPM=siz(1); NSTE=size(XCor,1);
MresH=zeros(NBPM,NSTE);
MresV=zeros(NBPM,NSTE);
MresVH=zeros(NBPM,NSTE);
MresHI=zeros(NBPM,NSTE);
MresVI=zeros(NBPM,NSTE);
MresHQ=zeros(NBPM,NSTE);
MresVQ=zeros(NBPM,NSTE);
MresHVQ=zeros(NBPM,NSTE);
%for i=1:NSTE
% CorNSTEXstuf(i,1:siz(2))=XCor(i,1:siz(2))-mean(XCor(i,:));
%end
CorNSTEXstuf=XCor-repmat(mean(XCor,2)',siz(2),1)';
%[Cor96Xstuf,Cor96Zstuf ] = SortCorrXZstuf( XZCor );
for i=1:96
Cor96Xstuf(i,1:siz(2))=XCor(i,1:siz(2))-mean(XCor(i,:));
end
[ CXI,CXQ,firstX,lastX,lengthX] = Corfast_fda2(Cor96Xstuf,Xamp,Xamp/20);
%[ CZI,CZQ,firstZ,lastZ,lengthZ] = Corfast_fda2(Cor96Zstuf,Zamp,Zamp/20);
disp(['Xamp=' num2str(Xamp) ...
' # steerers=', num2str(size(CorNSTEXstuf,1)) ...
' buffer size=',num2str(size(CorNSTEXstuf,2)) ])
[ CXI,CXQ,firstX,lastX,lengthX] = Corfast_fda2(CorNSTEXstuf,Xamp,Xamp/20);
Xres=zeros(1,siz(2));
Zres=zeros(1,siz(2));
CX=zeros(1,siz(2));
CZ=zeros(1,siz(2));
for j=1:96
j
for k=1:224
Xres(1:siz(2))=Xsnif(k,1:siz(2));
Zres(1:siz(2))=Zsnif(k,1:siz(2));
CX(1:siz(2))=CXI(j,1:siz(2));
%CZ(1:siz(2))=CZI(j,1:siz(2));
C=CX/sum(abs(CX));
A=max(abs(CX));
MresHI(k,j)=MresH(k,j)+Xres*C'/A;
MresVI(k,j)=MresV(k,j)+Zres*C'/A;
[ CX] = CXQ(j,1:siz(2));
C=CX/sum(abs(CX));
A=max(abs(CX));
MresHQ(k,j)=MresH(k,j)+Xres*C'/A;
MresVQ(k,j)=MresV(k,j)+Zres*C'/A;
end
end
C=CXI./repmat(sum(abs(CXI),2),1,siz(2));
A=max(abs(CXI),[],2);
MresHI=Xsnif*C'./repmat(A',NBPM,1);
MresVI=Zsnif*C'./repmat(A',NBPM,1);
CQ=CXQ./repmat(sum(abs(CXQ),2),1,siz(2));
AQ=max(abs(CXQ),[],2);
MresHQ=Xsnif*CQ'./repmat(AQ',NBPM,1);
MresVQ=Zsnif*CQ'./repmat(AQ',NBPM,1);
% for j=1:NSTE
% j
%
% CX(1:siz(2))=CXI(j,1:siz(2));
% C=CX/sum(abs(CX))/max(abs(CX));
%
% [ DX] = CXQ(j,1:siz(2));
% D=DX/sum(abs(DX))/max(abs(DX));
%
% for k=1:NBPM
%
% Xres(1:siz(2))=Xsnif(k,1:siz(2));
% Zres(1:siz(2))=Zsnif(k,1:siz(2));
%
%
% MresHI(k,j)=MresH(k,j)+Xres*C';
% MresVI(k,j)=MresV(k,j)+Zres*C';
%
% MresHQ(k,j)=MresH(k,j)+Xres*D';
% MresVQ(k,j)=MresV(k,j)+Zres*D';
%
% end
%
% end
stdHI=std(MresHI);
stdHQ=std(MresHQ);
......@@ -85,7 +82,7 @@ phi=stdHQ./sqrt(stdHI.^2+stdHQ.^2);
for i=1:96
for i=1:NSTE
if phi(i)< .8
MresH(:,i)=MresHI(:,i)*normI(i)/G;
MresVH(:,i)=MresVI(:,i)*normI(i)/G;
......@@ -98,6 +95,6 @@ end
time=time_label;
file=['TimeLabelMresX',time];
save(fullfile(qemres.datadir,file),'n_and_dateX' , 'MresH','MresVH')
save(fullfile(qemres.datadir,file),'n_and_dateX' , 'MresH','MresVH');
clock-clockstart
\ No newline at end of file
toc
\ No newline at end of file
function [normI,phi,firstZ,MresHV,MresV] = CalMresfdaV(n_and_dateZ,Zamp,qemres)
%uses calculated sine and cosine instead of fda Cor96XZstuf signals with a shift by 22 samples for the Q Cor signal
%uses calculated sine and cosine instead of fda CorNSTEXZstuf signals with a shift by 22 samples for the Q Cor signal
% to get the Iand Q data
% set_path
clockstart=clock
% set_path % TO BE COMMENTED OUT WHEN MERGING WITH QEMPANEL
tic
%load lastMresV
set_path
[Xsnif, Zsnif] =getFDAreaderXZ(n_and_dateZ);
[XCor, ZCor] =getFDAreaderXZCor(n_and_dateZ);
siz=size(Xsnif)
MresH=zeros(224,96);
MresV=zeros(224,96);
MresHV=zeros(224,96);
MresHI=zeros(224,96);
MresVI=zeros(224,96);
MresHQ=zeros(224,96);
MresVQ=zeros(224,96);
MresHVQ=zeros(224,96);
%[Cor96Xstuf,Cor96Zstuf ] = SortCorrXZstuf( XZCor );
for i=1:96
%Cor96Xstuf(i,1:siz(2))=XCor(i,1:siz(2))-mean(XCor(i,:));
Cor96Zstuf(i,1:siz(2))=ZCor(i,1:siz(2))-mean(ZCor(i,:));
end
%[ CXI,CXQ,firstX,lastX,lengthX] = Corfast_fda2(Cor96Xstuf,Xamp,Xamp/20);
[ CZI,CZQ,firstZ,lastZ,lengthZ] = Corfast_fda2(Cor96Zstuf,Zamp,Zamp/10);
Xres=zeros(1,siz(2));
Zres=zeros(1,siz(2));
CX=zeros(1,siz(2));
CZ=zeros(1,siz(2));
for j=1:96
j
for k=1:224
Xres(1:siz(2))=Xsnif(k,1:siz(2));
Zres(1:siz(2))=Zsnif(k,1:siz(2));
%CX(1:siz(2))=CXI(j,1:siz(2));
CZ(1:siz(2))=CZI(j,1:siz(2));
C=CZ/sum(abs(CZ));
A=max(abs(CZ));
MresHI(k,j)=MresH(k,j)+Xres*C'/A;
MresVI(k,j)=MresV(k,j)+Zres*C'/A;
[ CZ ] = CZQ(j,1:siz(2));
C=CZ/sum(abs(CZ));
A=max(abs(CZ));
MresHQ(k,j)=MresH(k,j)+Xres*C'/A;
MresVQ(k,j)=MresV(k,j)+Zres*C'/A;
end
end
siz=size(Xsnif);
NBPM=siz(1); NSTE=size(XCor,1);
MresH=zeros(NBPM,NSTE);
MresV=zeros(NBPM,NSTE);
MresHV=zeros(NBPM,NSTE);
MresHI=zeros(NBPM,NSTE);
MresVI=zeros(NBPM,NSTE);
MresHQ=zeros(NBPM,NSTE);
MresVQ=zeros(NBPM,NSTE);
MresHVQ=zeros(NBPM,NSTE);
% for i=1:NSTE
% CorNSTEZstuf(i,1:siz(2))=ZCor(i,1:siz(2))-mean(ZCor(i,:));
% end
CorNSTEZstuf=ZCor-repmat(mean(ZCor,2)',siz(2),1)';
disp(['Zamp=' num2str(Zamp) ...
' # steerers=', num2str(size(CorNSTEZstuf,1)) ...
' buffer size=',num2str(size(CorNSTEZstuf,2)) ])
[ CZI,CZQ,firstZ,lastZ,lengthZ] = Corfast_fda2(CorNSTEZstuf,Zamp,Zamp/10);
Xres=zeros(1,siz(2));
Zres=zeros(1,siz(2));
CX=zeros(1,siz(2));
CZ=zeros(1,siz(2));
C=CZI./repmat(sum(abs(CZI),2),1,siz(2));
A=max(abs(CZI),[],2);
MresHI=Xsnif*C'./repmat(A',NBPM,1);
MresVI=Zsnif*C'./repmat(A',NBPM,1);
CQ=CZQ./repmat(sum(abs(CZQ),2),1,siz(2));
AQ=max(abs(CZQ),[],2);
MresHQ=Xsnif*CQ'./repmat(AQ',NBPM,1);
MresVQ=Zsnif*CQ'./repmat(AQ',NBPM,1);
% for j=1:NSTE
% j
%
% for k=1:NBPM
%
% Xres(1:siz(2))=Xsnif(k,1:siz(2));
% Zres(1:siz(2))=Zsnif(k,1:siz(2));
% %CX(1:siz(2))=CXI(j,1:siz(2));
% CZ(1:siz(2))=CZI(j,1:siz(2));
%
% C=CZ/sum(abs(CZ));
% A=max(abs(CZ));
%
%
%
% MresHI(k,j)=MresH(k,j)+Xres*C'/A;
% MresVI(k,j)=MresV(k,j)+Zres*C'/A;
%
%
% [ CZ ] = CZQ(j,1:siz(2));
%
%
% C=CZ/sum(abs(CZ));
% A=max(abs(CZ));
%
%
% MresHQ(k,j)=MresH(k,j)+Xres*C'/A;
% MresVQ(k,j)=MresV(k,j)+Zres*C'/A;
%
%
% end
%
% end
stdHI=std(MresHI);
stdHQ=std(MresHQ);
......@@ -85,7 +93,7 @@ phi=stdVQ./sqrt(stdVI.^2+stdVQ.^2);
for i=1:96
for i=1:NSTE
if phi(i)< .8
MresV(:,i)=MresVI(:,i)*normI(i)/G;
MresHV(:,i)=MresHI(:,i)*normI(i)/G;
......@@ -98,6 +106,6 @@ end
time=time_label;
file=['TimeLabelMresZ',time];
save(fullfile(qemres.datadir,file),'n_and_dateZ' , 'MresV','MresHV')
save(fullfile(qemres.datadir,file),'n_and_dateZ' , 'MresV','MresHV');
clock-clockstart
\ No newline at end of file
toc
\ No newline at end of file
function[ CorI,CorQ,first,last,length] = Corfast_fda2( Cor,CorAmp ,mx)
%generate a signal CorQ pi/2 shifted in phase with respect to the sine signal Cor; CorI and corQ are true sine and cosine signals
%at the frequencies used for each corrector by the script getAllSnifferXZandCorTestSigNew2.
%ftab=[1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,1,1,1,1,1,1,1,1,1];
sizeCor=size(Cor);
NSTE=sizeCor(1);
if NSTE ~= 96
error('in Corfast_fda2 # of steerers must be 96, because of reordering from cell 1 to cell 4 at the end of the script');
end
n=sizeCor(2);
amp=4*CorAmp;
CorQ=zeros(NSTE,n);
CorI=zeros(NSTE,n);
first=20*ones(1,NSTE);
last=zeros(1,NSTE);
length=zeros(1,NSTE);
for i=1:NSTE
m=0;
for j=5:n
if (abs(Cor(i,j)<mx && m==0 ))||((abs(Cor(i,j))>=mx && abs(Cor(i,j+25))<CorAmp*2 && m==0))
first(i)=j+1;
else
first(i);
length(i)=5071;
m=1;
end
end
%First=first(i)
end
%for i=1:48
%for j=1:2
%if first(2*(i-1)+j)<74932
%fir=first(2*(i-1)+j);
%ps=round(length(i)/256);
%CorI(2*(i-1)+j,fir:fir+5066+j)=Cor(2*(i-1)+j,fir:fir+5066+j);
%CorQ(2*(i-1)+j,fir+ps:fir+5066+j)=CorI(2*(i-1)+j,fir:fir+5066+j-ps);
%CorQ(2*(i-1)+j,fir:fir+ps-1)=-CorI(2*(i-1)+j,fir+5066+j-ps+1:fir+5066+j);
%else
%CorI(2*(i-1)+j,:)=Cor(2*(i-1)+j,:);
% end
%end
%end
for i=1:NSTE
if first(i)<n-5072
sp=abs(fft(Cor(i,first(i):first(i)+5071)));
[mf,k]=max(sp(1:5071));
f=((k-1)*2);
for j=1:5072
%CorI(i,first(i)+j-1)=amp*sin(2*pi*j*(114+2*ftab(i))/10135) ;
%CorQ(i,first(i)+j-1)=amp*cos(2*pi*j*(114+2*ftab(i))/10135) ;
CorI(i,first(i)+j-1)=amp*sin(2*pi*j*(f)/10144) ;
CorQ(i,first(i)+j-1)=amp*cos(2*pi*j*(f)/10144) ;
end
else
CorI(i,:)=Cor(i,:);
end
end
sizeCor=size(CorI);
Cor=zeros(NSTE,sizeCor(2));
Cor(1:87,:)=CorI(10:NSTE,:);
Cor(88:NSTE,:)=CorI(1:9,:);
CorI=Cor;
Cor=zeros(NSTE,sizeCor(2));
Cor(1:87,:)=CorQ(10:NSTE,:);
Cor(88:NSTE,:)=CorQ(1:9,:);
CorQ=Cor;
function [Xsnif, Zsnif] =getFDAreaderXZ(n_and_date)
%uses the fda data extractor to get Intensity n data recorded at the date
%"date"
%Extractor argin description: (DevVarStringArray)
%[0] Number of samples to read
%[1] Start date (ex: 22/06/2015 16:15:00[.103647], les us sont optionnelles)
%for instance: n_and_date = {'100','28/09/2015 08:00:00'} or {'100','28/09/2015 08:00:00.178546'}
n=str2double(n_and_date(1));
fda='tango:sr/d-bpmlibera/fda_reader';
devfda=dvopen(fda);
dvtimeout(devfda,50);
XZ=dvcmd(devfda,'ReadXZPositions',n_and_date);
pause(.1);
size(XZ);
X=XZ(1:224*n);
Z=XZ(1+(224*n):448*n);
Xsnif=reshape(X,224,n);
Zsnif=reshape(Z,224,n);
function [XCor, ZCor] =getFDAreaderXZCor(n_and_date)
%uses the fda data extractor to get Intensity n data recorded at the date
%"date"
%Extractor argin description: (DevVarStringArray)
%[0] Number of samples to read
%[1] Start date (ex: 22/06/2015 16:15:00[.103647], les us sont optionnelles)
%for instance: n_and_date = {'100','28/09/2015 08:00:00'} or {'100','28/09/2015 08:00:00.178546'}
n=str2double(n_and_date(1));
fda='tango:sr/d-bpmlibera/fda_reader';
devfda=dvopen(fda);
dvtimeout(devfda,50);
XZcor=dvcmd(devfda,'ReadXZCorrectionsSorted',n_and_date);
pause(.1);
siz2=size(XZcor);
siz=siz2(2);
Xcor=XZcor(1:siz/2);
Zcor=XZcor(1+siz/2:siz);
XCor=reshape(Xcor,96,siz/2/96);
ZCor=reshape(Zcor,96,siz/2/96);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment