-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGriffinAndLim_v5_int_logstft_votingIsh.m
176 lines (113 loc) · 4.83 KB
/
GriffinAndLim_v5_int_logstft_votingIsh.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
% Spectrogram function
addpath( 'C:\Users\Lord Photon\Documents\MATLAB\library_repo\library' )
addpath( '/Users/ben/Documents/MATLAB/library_repo' )
%% Time frequency vectors definition
lent=2^14; % Signal length
tWind=8e-9; % Time window span
t=linspace(0,tWind,lent);
dt=t(2)-t(1);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;
%% SUT generation
fmax=300e9/2;%Fs/10;
% SUTf=superGauss(0,fmax,10,f,0).*(exp(1j*(tWind/4/(fmax*2*pi))*(2*pi*f).^2/2));
SUTf=superGauss(0,fmax,10,f,0).*(exp(1j*(240*22e-24/2)*(2*pi*f).^2/2));%+...
% superGauss(0,fmax,10,f,0).*(exp(-1j*(240*22e-24/2)*(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);
% SUT=ones(size(SUT));
%% window Setup
fMaxStft=400e9;
filtm=10;
plotFilt=0;
% Adjust these parameters as needed
winLen=2^7;
winLent=winLen*dt
winInc=winLen;%winLen-1;%/(2^2);
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);
windowInds=(1:lent)-lent/2;
% win=superGauss(0,winLen/2,100,windowInds,0);
win=zeros(1,lent); win(round(lent/2)-winLen/2:round(lent/2)+winLen/2-1)=winSec;
win=circshift(win,lent/2);
% No need to change the ones below
nIncs=round(lent/winInc); % By making winInc a power of 2, we can make sure to have an integer number of windows.
windowCenters=(1:nIncs)*winInc;
%% Spectrogram Algorithm
% Get spgm from windowIncrease Above
stft=get_stft_fullSigLen(nIncs,windowCenters,lent,win,dt,SUT);
spgmRaw=abs(stft).^2;
fspgm_raw=f;
tspgm_raw=linspace(t(1),t(end),numel(stft(1,:)));
%%% PLaying around with COLA
% sutRecon=get_istft_fullSigLen(lent,windowCenters,win/(sum(win)/winInc),Fs,nIncs,stft);
% figure;
% for i=1:winInc
% plot(circshift(win,windowCenters(i)),'--')
% hold on
% end
% figure;imagesc(spgmRaw);
% figure;plot(sutRecon); hold on; plot(circshift(win,lent/2))
% Setup interpolation
%% Setup interpolation
nIncsInterp=nIncs*interpAmount_t;
windowCentersInterp=(1:nIncsInterp)*winInc/interpAmount_t;
tspgm=linspace(t(1),t(end),numel(stft(1,:))*interpAmount_t);
fspgm=linspace(f(1),f(end),numel(fspgm_raw)*interpAmount_f);%fspgm_raw;
spgm=interp2fun(tspgm_raw,fspgm,spgmRaw,tspgm,fspgm);
winLenInterp=numel(fspgm)*interpAmount_f;
winInterp=interp1(linspace(0,1,lent),win,linspace(0,1,winLenInterp));
winIndsInterp=(1:numel(fspgm))-round(numel(fspgm)/2);
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
%% Iterative Griffin and Lim algorithm
% % % Run many times; combine outputs for final iteration
nIter=10;
SiOut=zeros(numel(spgm(:,1)),numel(spgm(1,:)),nIter);
xtOut=zeros(numel(SUT),nIter);
for iIter=1:nIter
S0=sqrt(spgm).*exp(1j*rand(size(spgm))*2*pi);%.*(-1*(stft<0));%.*exp(1j*rand(size(spgm))*2*pi); % Seed stft
xt0=get_istft_fullSigLen(lent,windowCentersInterp,analysisWin,Fs,nIncsInterp,S0);
% xt0=get_istft_fullSigLen(lent,windowCentersInterp,analysisWin,Fs,nIncsInterp,stft);
maxIteration=40;
% Convergence criterion
di=zeros(1,maxIteration);
diC=di;
diR=di;
plotIter=0;
if iIter==1; plotIter=1;end
% plotIter=1;
[xt,Si]=phaseRecovLoop(nIncsInterp,windowCentersInterp,lent,winInterp,winLen,t,dt,xt0,tspgm,fspgm,spgm,SUT,analysisWin,Fs,maxIteration,{'mse','frogE'},plotIter,filtm,fMaxStft,plotFilt,f);
%
SiOut(:,:,iIter)=Si;
xtOut(:,iIter)=xt;
iIter
end
SiOut_Raw=SiOut;
for j=1:nIter;
SiOut(:,:,nIter)=SiOut(:,:,nIter).*exp(-1j*angle(SiOut(end/2,end/2,nIter))).*exp(1j*angle(angle(SiOut(end/2,end/2,1))));
end
angle(SiOut(end/2,end/2,:))
pairs=nchoosek(1:nIter,2);
SiDiffs=zeros(numel(spgm(:,1)),numel(spgm(1,:)),numel(pairs(:,1)));
for iVot=1:numel(pairs(:,1))
SiDiffs(:,:,iVot)=abs(SiOut(:,:,pairs(iVot,1))-SiOut(:,:,pairs(iVot,2)));
end
[~,minPair]=min(SiDiffs,[],3);
pairs1=pairs(:,1); pairs2=pairs(:,2);
allPtsPairs1=pairs1(minPair);allPtsPairs2=pairs2(minPair);
% SiMean=mean(SiPairOpt,3);
[m,n,k] = size(SiOut);
[q,w] = ndgrid(1:m,1:n);
Sipair1 = SiOut(sub2ind([m,n,k],q,w,allPtsPairs1));
Sipair2 = SiOut(sub2ind([m,n,k],q,w,allPtsPairs2));
SiPairOpt=mean(cat(3,Sipair1,Sipair2),3);
% SiMean=mean(SiOut,3);
xt0=get_istft_fullSigLen(lent,windowCentersInterp,analysisWin,Fs,nIncsInterp,SiPairOpt);
plotIter=1;
% xt0=mean(xtOut,2).';
maxIteration=40;
% xt0=get_istft_fullSigLen(lent,windowCentersInterp,analysisWin,Fs,nIncsInterp,sqrt(spgm).*exp(1j*angle(SiMean)));
[xt,Si]=phaseRecovLoop(nIncsInterp,windowCentersInterp,lent,winInterp,winLen,t,dt,xt0,tspgm,fspgm,spgm,SUT,analysisWin,Fs,maxIteration,{'mse','frogE'},plotIter,filtm,fMaxStft,plotFilt,f);