-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGriffinAndLim_v6_experimentalData.m
309 lines (224 loc) · 9.18 KB
/
GriffinAndLim_v6_experimentalData.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
% Spectrogram function
addpath( 'C:\Users\Lord Photon\Documents\MATLAB\library_repo\library' )
addpath( '/Users/ben/Documents/MATLAB/library_repo' )
addpath('../phaseRecovery_Data');
%% Load data and set time-frequency vectors
% load('/Users/ben/Documents/MATLAB/timeFrequencyAnalysis/phaseRecovery_Data/RTOZigZag.mat');
% load('/Users/ben/Documents/MATLAB/timeFrequencyAnalysis/phaseRecovery_Data/RTOzigzag_deconv.mat');
% load('C:\Users\Lord Photon\Documents\MATLAB\time-frequency analysis\PhaseRecoveryAlgorithms_repo\phaseRecovery_Data/RTOZigZag.mat');
% load('RTOzigzag_deconv.mat');
% tIndsExpInterest=2:179;
% fSpecGHz=fSpecGHz;%/(56.4e9*TargetResolution)
% winLen_t=200e-12
% lowerClip=max(max(spgm))/15;
% nptPerWin=32;
% phaseAnalysis=1;
% nFreqElem=numel(fSpecGHz);%2000;
% freqInds=round(linspace(1,numel(fSpecGHz),nFreqElem));
% %
load('OSOdataCross_filtv2.mat');
% load('OSOdataCross.mat');
tIndsExpInterest=70:160;
winLen_t=62.5e-12
lowerClip=max(max(spgm))/2.5;
nptPerWin=64;
phaseAnalysis=2;
fMaxTLS=fSpecGHz(end)*1e9*2;
spgmIni=spgm;%.^0.8;%(freqInds,:);
% restrict time axis and clip lower values to avoid noise issues.
spgmExpRaw=spgmIni(:,tIndsExpInterest);
spgmExpRaw(spgmExpRaw<lowerClip)=0;%
spgmExpRaw(spgmExpRaw>lowerClip)=spgmExpRaw(spgmExpRaw>lowerClip)-lowerClip;
%% SUT generation
% Used to compared with a simulation SUT... doesn't seem very relevant.
% fmax=190e9;
% SUTf=superGauss(0,fmax,4,f,0).*(exp(1j*(105*22e-24)*(2*pi*f).^2/2))+superGauss(0,fmax,10,f,0).*(exp(-1j*(105*22e-24)*(2*pi*f).^2/2));
% SUTf=superGauss(0,sutBW,10,f,0).*(exp(1j*(tWind/4/(sutBW*2*pi))*(2*pi*f).^2/2))+...
% superGauss(0,sutBW,10,f,0).*(exp(-1j*(tWind/4/(sutBW*2*pi))*(2*pi*f).^2/2));
% SUT=nifft(SUTf,Fs);
%% Time frequency vectors definition
fSpecExp=fSpecGHz*1e9; tSpecExp=tSpecns(tIndsExpInterest)*1e-9;
% Setup time-frequency axes
%2^nextpow2(numel(fSpecExpRaw));
%%% Used to have a +1 in the tWind???? % tWind=winLen_t*round(1+(tSpecExp(end)-tSpecExp(1))/winLen_t); % The total length of t dictates the minimum required frequency resolution
tWind=numel(tSpecExp)*winLen_t;%(tSpecExp(end)-tSpecExp(1));%winLen_t*round((tSpecExp(end)-tSpecExp(1))/winLen_t); % The total length of t dictates the minimum required frequency resolution
TargetResolution=winLen_t/nptPerWin;%1/(extraFreqNeeded*2*fSpecExpRaw(end));%448e9;%winLen_t/(2^5); % Target temporal resolution of the STFT (i.e., frequency span)
% % % Used to force to round by 2 % % % freqPadNeeded=round((1/TargetResolution-2*fSpecExpRaw(end))*tWind/2)*2; %%%%%%%%%% rounding to 2 because I'm lazy now. is this important?
% freqPadNeeded=round((1/TargetResolution-2*fSpecExpRaw(end))/2*tWind)*2; %%%%%%%%%% rounding to 2 because I'm lazy now. is this important?
lent=round(tWind/TargetResolution);
% % 0pad in the time domain
% nSlicesPad=2^8;
% tPad=nptPerWin*nSlicesPad;
% spgmExpRaw=[zeros(numel(fSpecExp),nSlicesPad/2),spgmExpRaw,zeros(numel(fSpecExp),nSlicesPad/2)];
% lent=tPad+lent; tSpecExp=(1:numel(spgmExpRaw(1,:)))*winLen_t;tWind=tWind+nSlicesPad*winLen_t;
dt=tWind/lent;tWind=lent*dt;
t=(1:lent)*dt;
Fs=1/dt;f=linspace(-Fs/2,Fs/2,lent);df=(f(2)-f(1));
fG=f*10^-9;tps=t*10^12;%GHz
scale=1;
fspgm_raw=f;
tspgm_raw=tSpecExp;
SUT=zeros(1,lent);
% Zero pad if needed
if 1/(2*TargetResolution)>fSpecExp(end)
[a,nZeros]=find(f>-abs(fMaxTLS)/2,1);
f_tls1=[f(1:nZeros-1),fSpecExp,-flip(f(1:nZeros-1))];
padded1=[zeros(nZeros-1,numel(spgmExpRaw(1,:)));spgmExpRaw;zeros(nZeros-1,numel(spgmExpRaw(1,:)))];
spgmExpRawPadded=interp2fun(tspgm_raw,f_tls1,padded1,tspgm_raw,f);
% spgmExpRawPadded=imresize(padded1,[numel(tspgm_raw),numel(f)]);
spgmExpRawPadded(1:nZeros,:)=0;spgmExpRawPadded(lent-nZeros:end,:)=0;
% fSpecExpRawPadded=[-1/(2*TargetResolution),fSpecExp,1/(2*TargetResolution)];%(1:lent)*dfSER-1/(2*TargetResolution);
% spgmExpRawPadded=[zeros(1,numel(tSpecExp));spgmExpRaw;zeros(1,numel(tSpecExp))]; % If freqPadNeeded/2 doesn't work this will cause an error
else
fSpecExpRawPadded=fSpecExp; spgmExpRawPadded=spgmExpRaw;
end
%% window Setup
winLen=winLen_t/dt;
if abs(winLen-round(winLen))<0.01
winLen=round(winLen)
warning('small rounding of winLen')
end
winInc=winLen;
interpAmount_t=1 ; % For now, make this a power of 2 (or 1)!!
interpAmount_f=1 ; % For now, make this a power of 2 (or 1)!!
winSec=ones(1,winLen);
% win=superGauss(0,winLen/2,2,windowInds,0);
win=zeros(1,lent); win(round(lent/2)-floor(winLen/2):round(lent/2)+ceil(winLen/2)-1)=winSec;
win=circshift(win,lent/2);
% No need to change the ones below
nIncs=lent/winInc; % By making winInc a power of 2, we can make sure to have an integer number of windows.
windowCenters=(1:nIncs)*winInc;
%% Setup interpolation
nIncsInterp=nIncs*interpAmount_t;
windowCentersInterp=(1:nIncsInterp)*winInc/interpAmount_t;
winLenInterp=numel(fspgm_raw)*interpAmount_f;
winInterp=interp1(linspace(0,1,lent),win,linspace(0,1,winLenInterp));
overlapAmount=interpAmount_t*(sum(win))/winInc;%numel(winIndsInterp)/(windowCentersInterp(2)-windowCentersInterp(1)); % This is the "overlapamount" AFTER interpolation
analysisWin=winInterp/overlapAmount; % Analysis window for the inverse spgm
tspgm=linspace(tspgm_raw(1),tspgm_raw(end),numel(tspgm_raw)*interpAmount_t);
fspgm=linspace(fspgm_raw(1),fspgm_raw(end),numel(fspgm_raw)*interpAmount_f);%fspgm_raw;
% fspgm=fspgm_raw;
% fspgm=fSpecExpRawPadded;
% spgm=griddata(tspgm_rawM,fspgm_rawM,spgmRaw,tspgmM,fspgmM,'natural');
% spgminterp1=interp2fun(tspgm_raw,fspgm,spgmRaw,tspgm,fspgm);
%
% spgm=spgminterp1;
% % % spgm=abs(interp2fun(tspgm_raw,fSpecExpRawPadded,spgmExpRawPadded,tspgm,fspgm));
if interpAmount_t>1
spgm=abs(imresize(spgmExpRawPadded,[lent,numel(tspgm)]));
else
spgm=spgmExpRawPadded;
end
%
% % Remove the extrapolated noise
% numToDecimate=round((1/(2*dt)-fSpecExp(end))/df);
% spgm(1:numToDecimate,:)=0; spgm(end-numToDecimate:end,:)=0;
%% for smoothing out the spgm
% sigma=1;
% % lowerClip=7.7;
% % spgm(spgm<lowerClip)=0;%
% % spgm(spgm>lowerClip)=spgm(spgm>lowerClip)-lowerClip;
% spgminterp1=spgminterp1-mean(spgminterp1(:,1));
% spgm= imgaussfilt(spgminterp1,sigma);
figure;
subplot(3,1,1)
imagesc(tSpecns,fSpecGHz,spgmIni);
% subplot(3,1,2)
% imagesc(spgminterp1)
subplot(3,1,3)
imagesc(tspgm,fspgm,spgm)
%% Iterative Griffin and Lim algorithm
S0=sqrt(spgm).*exp(1j*rand(size(spgm))*2*pi);%.*(-1*(stft<0));%.*exp(1j*rand(size(spgm))*2*pi); % Seed stft
% figure;plot(tspgm,sum(S0,1))
iniPhase=exp(1j*normrnd(0,pi,size(S0)));
xt0=get_istft_fullSigLen(lent,windowCentersInterp,analysisWin,Fs,nIncsInterp,S0.*iniPhase);
maxIteration=45;
% Convergence criterion
di=zeros(1,maxIteration);
diC=di;
diR=di;
plotIter=1;
[xt,Si]=phaseRecovLoop(nIncsInterp,windowCentersInterp,lent,winInterp,winLen,t,dt,xt0,tspgm,fspgm,spgm,SUT,analysisWin,Fs,maxIteration,{'Inconsistency','frogE'},plotIter);
xt1=xt;%
%
% figure;
% plot(t,real(xt));
% hold on ;
% plot(t,abs(xt));
% yyaxis right;
% plot(t,unwrap(angle(xt)))
%
%% Phase analysis
%
%
sec1t=118:554;
sec1f=192:257;
xt=filtSG_tf(xt1,t,f,30e9/df,2,1);
% sec1t=554:1391;
% sec1f=391:465;
t1=t(sec1t);
xt1=xt(sec1t);
f1=linspace(-Fs/2,Fs/2,numel(t1));
xf1=(nfft(xt1));
fitObj1=fit(2*pi*f1(sec1f)',unwrap(angle(xf1(sec1f)))','poly2');
figure;plot(2*pi*f1,unwrap(angle(nfft(xt1)))); hold on; plot(2*pi*f1(sec1f),fitObj1(2*pi*f1(sec1f)));
figure;plot(unwrap(angle((xt1))));
beta2=fitObj1.p1*2
beta2/22e-24
%% Phase analysis
if phaseAnalysis==1
% xt=xt(4111:9808);t=t(4111:9808);
recovPhaseRaw=unwrap(angle(xt));
linFit=fit(t',recovPhaseRaw','poly1')
recovPhaseflat=recovPhaseRaw-linFit(t)';
recovPhase=recovPhaseflat;
% recovPhase=real(filtSG_tf(recovPhaseflat,t,f,200,2,1));
figure;plot(recovPhase)
% fitReg={1:721,860:1590};
[~,locsTop]=findpeaks(recovPhase,'MinPeakProminence',50);
[~,locsBot]=findpeaks(-recovPhase,'MinPeakProminence',50);
rangeTop=round(7*winLen_t/dt); rangeBot=round(3*winLen_t/dt);
topInds=((1:rangeTop)-rangeTop/2)+locsTop';
botInds=((1:rangeBot)-rangeBot/2)+locsBot';
fitReg={topInds,botInds};
figure;
subplot(3,1,1)
plot(t*1e9,recovPhase); hold on
xlim([t(1) t(end)]*1e9);
for ifit=1:2
% iFit=2;
tfits=t(fitReg{ifit}); yfits=recovPhase(fitReg{ifit});
beta2=[];
for i=1:numel(tfits(:,1))
tfit=tfits(i,:); yfit=yfits(i,:);
fitObj=fit(tfit',yfit','poly2')
beta2(i)=1/(2*fitObj.p1)
% pause(0.3)
plot(tfit*1e9,yfit); plot(tfit*1e9,fitObj(tfit));plot( tfit(round(numel(yfit)/2))*1e9,yfit(round(numel(yfit)/2)),'*')
end
beta2s{ifit}=beta2;
end
ylabel('Phase (rad)');
yyaxis right
plot(t*1e9,abs(xt).^2/(max(abs(xt).^2)))
xlabel('time (ns)')
subplot(3,1,2)
plot(t(topInds(:,rangeTop/2))*1e9,beta2s{1},'*')
hold on
plot(t(topInds(:,rangeTop/2))*1e9,mean(beta2s{1})*ones(1,numel(beta2s{1})))
title(['mean beta2: ' num2str(mean(beta2s{1})*1e24) 'ps2 (' num2str(mean(beta2s{1})*1e24/22) ' km SMF)'])
xlim([t(1) t(end)]*1e9);
subplot(3,1,3)
plot(t(botInds(:,rangeBot/2))*1e9,beta2s{2},'*')
hold on
plot(t(botInds(:,rangeBot/2))*1e9,mean(beta2s{2})*ones(1,numel(beta2s{2})))
title(['mean beta2: ' num2str(mean(beta2s{2})*1e24) 'ps2 (' num2str(mean(beta2s{2})*1e24/22) ' km SMF)'])
xlim([t(1) t(end)]*1e9);
end
if phaseAnalysis==2
figure;
plot(t,abs(xt).^2); ylabel('intensity')
yyaxis right
plot(t,unwrap(angle(xt))); ylabel('angle (rad)')
xlabel('time (ns)')
end