-
Notifications
You must be signed in to change notification settings - Fork 16
/
AlignUsingCr.m
executable file
·76 lines (74 loc) · 3.48 KB
/
AlignUsingCr.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function AllFramesFTrealign=AlignUsingCr(AllFramesFTrealign,MRS_struct)
%Using the freq information from MRS_struct, determine appropriate range to
%fit Cr signal over
Cr_ppm=3.02;
%Determine limits
z=abs(MRS_struct.freq-3.12);
lb=find(min(z)==z);
z=abs(MRS_struct.freq-2.72);
ub=find(min(z)==z);
%Set initial parameters by fitting the sum
freqrange = MRS_struct.freq(lb:ub);
Initx = [ 30 0.05 3.02 0 0 0 ];
SumSpec = sum(AllFramesFTrealign(lb:ub,:),2);
SumSpecParams = FitPeaksByFrames2(freqrange, SumSpec, Initx); %FitPeaksByFrames2 now edited so that output params have same units as Initx
%Update freq and phase of AllFramesFTrealign to reflect Sum fit
AllFramesFTrealign=AllFramesFTrealign*exp(1i*SumSpecParams(4));
%Shift Cr freq to 3.02
freq_step_size=abs(MRS_struct.freq(1)-MRS_struct.freq(2));
Shift_points=round((real(SumSpecParams(3))-Cr_ppm)/freq_step_size)
AllFramesFTrealign=circshift(AllFramesFTrealign,[Shift_points 0]);
%Set initial fitting parameters by fitting the sum
Init = SumSpecParams./[ size(AllFramesFTrealign,2) 1 1 1 size(AllFramesFTrealign,2) size(AllFramesFTrealign,2) ];
Init(3)=Cr_ppm;
Init(4)=0;
%Select Data2BeFit
%subplot(2,2,1)
%plot(MRS_struct.freq,real(AllFramesFTrealign))
%set(gca,'XLim',[2.5 3.5]);
%set(gca,'XDir','reverse')
Data2bFit = AllFramesFTrealign(lb:ub,:);
FrameParams = FitPeaksByFrames2(freqrange, Data2bFit, Init); %FitPeaksByFrames2 now edited so that output params have same units as Initx
%Update freq and phase of AllFramesFTrealign to reflect Frame-by-Frame fit
FrameShift_points=round((real(FrameParams(:,3))-Cr_ppm)/freq_step_size);
for jj=1:size(FrameShift_points,1)
AllFramesFTrealign(:,jj)=circshift(AllFramesFTrealign(:,jj),[-FrameShift_points(jj) 0]);
end
%subplot(2,2,2)
%plot(MRS_struct.freq,real(AllFramesFTrealign),'.-')
%set(gca,'XLim',[2.5 3.5]);
%set(gca,'XDir','reverse')
%mean(FrameShift_points)
%mean(FrameParams(:,3))
%size(AllFramesFTrealign)
%size(repmat(exp(1i*SumSpecParams(:,4)).',[length(MRS_struct.freq) 1]))
AllFramesFTrealign=AllFramesFTrealign.*repmat(exp(-1i*FrameParams(:,4)).',[length(MRS_struct.freq) 1]);
AllFramesFTrealign=AllFramesFTrealign+repmat(FrameParams(:,5).',[length(MRS_struct.freq) 1]);
%subplot(2,2,3)
%plot(MRS_struct.freq,real(AllFramesFTrealign))
%set(gca,'XLim',[2.5 3.5]);
%set(gca,'XDir','reverse');
%plot the models.
Models=zeros(size(Data2bFit));
for ii=1:size(Models,2)
Models(:,ii)=LorentzModel(FrameParams(ii,:),freqrange);
end
subplot(3,1,1)
plot(freqrange,Models);
set(gca,'XLim',[2.5 3.5]);
set(gca,'XDir','reverse');
for ii=1:size(Models,2)
%Models_undone(:,ii)=((Models(:,ii)-FrameParams(ii,5)-FrameParams(ii,6)*(freqrange.'-FrameParams(ii,3))))*exp(-1i*FrameParams(ii,4));
Models_undone(:,ii)=(Models(:,ii)*exp(1i*FrameParams(ii,4)));
%Models_undone(:,ii)=circshift(Models_undone(:,ii),[FrameShift_points(jj) 0]);
end
subplot(3,1,2)
plot(freqrange,Models_undone);
set(gca,'XLim',[2.5 3.5]);
set(gca,'XDir','reverse');
subplot(3,1,3)
plot(1:length(freqrange),std(Models.'),1:length(freqrange),std(Models_undone.'))
plot(freqrange,sum(Data2bFit,2),freqrange,sum(Models_undone,2))
save testing.mat Data2bFit FrameParams freqrange
%plot(FrameParams(:,4)/pi*180)
end