-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeLSG_from_folder.m
313 lines (288 loc) · 8.32 KB
/
makeLSG_from_folder.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
310
311
312
function makeLSG_from_folder()
% plotDailyAvesSingleLSG_170715.m
%
% Single site long spectrogram use the same T and F vector.
% the power matrix for each site is pre-allocated, then filled for days
% when data exists
%
% 170707 smw
%
% clear variables
ns = 1; % number of sites
poff = datenum([2000 0 0 0 0 0]); % needed to show year as 2010 not 10
spflag = 1; % save plot flag yes=1, no=0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get user input
fs = 200000;
nch = 1;
% user input dialog box
prompt={'Enter Long Spectrogram Sample Rate : ',...
'Enter number of Channels (1 or 4) : '};
def={num2str(fs),...
num2str(nch)};
dlgTitle='Set Section Get Parameters';
lineNo=1;
AddOpts.Resize='on';
AddOpts.WindowStyle='normal';
AddOpts.Interpreter='tex';
in=inputdlg(prompt,dlgTitle,lineNo,def,AddOpts);
if isempty(in) % if cancel button pushed
return
end
%%%%%%%%%%%%%%%%%%%%%%%
fs = str2num(deal(in{1}));
nch = str2num(deal(in{2}));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set parameters based on sample rate
if fs == 2000 || fs == 3200
npF = 1001; % number of frequency bins
Fp = 0:1:1000;
la = 10; % plot min freq
lb = 1000; % plot max freq
% color min and max in spectral power
cmn = 39;
cmx = 110;
cmin = 1; cmax = 110; %[dB]
fstr = 'Hz';
ptype = 1; % high freq linear =0, low freq log =1
elseif fs == 10000
npF = 501; % number of frequency bins
%Fp = 0:0.01:5;
%la = 0; % plot min freq
%lb = 5; % plot max freq
% color min and max in spectral power
cmn = 50;
cmx = 80;
% cmin = 1; cmax = 80; %[dB]
% fstr = 'kHz';
ptype = 0; % high freq linear =0, low freq log =1
if ptype == 1
Fp = 0:10:5000;
la = 10; % plot min freq
lb = 5000; % plot max freq
fstr = 'Hz';
else
Fp = 0:0.01:5;
la = 0; % plot min freq
lb = 5; % plot max freq
fstr = 'kHz';
cmx = 70; %[dB]
end
elseif fs == 200000
npF = 1001; % number of frequency bins
Fp = 0:0.1:100;
la = 0; % plot min freq
lb = 100; % plot max freq
% color min and max in spectral power
cmn = 30;
cmx = 60;
cmin = 1; cmax = 70; %[dB]
fstr = 'kHz';
ptype = 0; % high freq linear =0, low freq log =1
elseif fs == 320000
npF = 1601; % number of frequency bins
Fp = 0:0.1:160;
la = 0; % plot min freq
lb = 160; % plot max freq
% color min and max in spectral power
cmn = 30;
cmx = 60;
cmin = 1; cmax = 70; %[dB]
fstr = 'kHz';
ptype = 0; % high freq linear =0, low freq log =1
else
disp(['Error, Unknown Sample Rate : ',num2str(fs)])
return
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% choose directory with *DailyAvesB.mat files named in increasing temporal
% order
%
fn_pathname = uigetdir('Pick Folder with *DailyAvesB.mat files');
if ~fn_pathname
disp('Canceled')
end
cd(fn_pathname)
fld = dir('*DailyAvesB.mat');
x = struct2cell(fld);
fn_files = x(1,:);
% read in spectra Aves for each site, already combined over deployments
%
% [fn_files, fn_pathname] = uigetfile('*.mat','Pick Site*_SpectralAves.mat Files','MultiSelect','on');
disp('Spectrum Levels for : ')
if iscell(fn_files) % then it's a cell full of filenames
fn = cell(length(fn_files),1);
for k = 1:length(fn_files)
fn{k} = fullfile(fn_pathname, fn_files{k});
disp(fn{k});
end
else % it's just one file, but put into cell
fn = cell(1,1);
fn{1} = fullfile(fn_pathname,fn_files);
disp(fn);
end
nS = length(fn); % number of *_DailyAves.mat files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% number of days to plot
load(fn{1})
ta = floor(pmptime(1)) + poff - 1;
load(fn{nS})
tb = ceil(pmptime(end)) + poff;
Tp = ta:1:tb;
dD = ta - poff;
nD = tb - ta + 1;
FM = zeros(npF,nD); % pre-allocate power matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% put site values in one matrix
%
sn{nS}=[];
tf_file = [];
figure(322);clf
meanPmp = [];
for k = 1:nS
load(fn{k}) % 'T','P','freq','MP','sname'
T = pmptime;
P = pmp;
sname = dname(7:end);
sn{k} = sname;
tf{k} = tf_file;
tf_file = []; % reset to empty because some files may not have this variable
deplStart{k} = T(1);
Ti = T - dD;
FM(:,Ti) = P(1:npF,:);
meanPmp(k,:) = smooth(mean(pmp,2),10);
figure(322);plot(freq,meanPmp(k,:));hold on
end
% make xTick for months
d=datevec(Tp);
%takes every month just once
[a,idxm]=unique(d(:,1:2),'rows');
%takes every year just once
[a,idxy]=unique(d(:,1),'rows');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% make figure
%%
hF=figure(750);clf
set(hF,'Units','normalized','Position',[.15,.2,.75,.7])
% pp = [0.25 0.25 8.0 10.5];
pp = [0.25 0.25 10.5 8.0];
set(hF,'PaperPosition',pp)
FS = 24;
FS2 = FS;
%%%%%%%%%%%%%%%%%%%%
% plot spectrogram
%
h = surf(Tp,Fp,FM,'Linestyle','none');
sgAx = get(h,'Parent');
view(sgAx,0,90)
if ptype
set(sgAx,'yscale','log')
else
set(sgAx,'yscale','linear')
end
set(sgAx,'TickDir','out')
axis([ta tb la lb])
set(gca,'FontSize',20)
% set(gca,'XtickLabel',[])
set(gca,'XtickLabel',Tp(idxm))
% set(gca,'xtick',Tp(idxy))
%set(gca,'xtick',Tp(idxm))
datetick('x','mmm-yy','keeplimits','keepticks')
% datetick
% % do not put 1st year label unless first month is Jan
% if d(1,2) ~= 1
% XtL = cellstr(get(gca,'XtickLabel'));
% if Tp(end)-Tp(1)<365
% XtL{1} = num2str(year(Tp(1)));
% else
% XtL{1} = blanks(1);
% end
% set(gca,'XtickLabel',XtL);
% end
% xLabel = cellstr(get(gca,'XTickLabel'));
% janIdx = find(strcmp(xLabel,'Jan'));
% if ~isempty(janIdx) % if deployment is less than a year, specify year on "Jan" label, if exists.
% xLabel{janIdx} = [ num2str(year(Tp(janIdx))), ' ',xLabel{janIdx}];
% set(gca,'XTickLabel',xLabel)
% xtickangle(45)
% end
ax = get(gca);
% minor ticks
ax.XAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = Tp(idxm);
ap = get(gca,'Position');
set(gca,'Position',[ap(1) ap(2)+0.1 ap(3) ap(4)*.80])
ylabel(['Frequency [',fstr,']'],'FontSize',FS)
% plot frame/box
txtz = 200;
% dtl = datenum([0 0 1 0 0 0]);
dtl = datenum([0 0 0 1 0 0]);
dfa = -1e-10; dfb = 1e-10;
lx = [ta+dtl ta+dtl tb-2*dtl tb-2*dtl ta+dtl];
ly = [la+dfa lb-dfb lb-dfb la+dfa la+dfa];
lz = txtz.*ones(1,5);
hold on
plot3(lx,ly,lz,'-k','LineWidth',2)
hold off
caxis([cmn cmx]) % set color limits
colormap(jet)
z_max = max(max(get(h,'Zdata')));
hold on
% add deployment name and tf name
for iDep = 1:length(deplStart)
text(deplStart{iDep}+poff,lb*1.06+1500*ptype,0,strrep(sn{iDep},'_','\_'))
if ~isempty(tf{iDep})
tfShort = strrep(tf{iDep},'_','\_');
text(deplStart{iDep}+poff,lb*1.03+500*ptype,0,tfShort(1:3))
line([deplStart{iDep}+poff,deplStart{iDep}+poff],...
[1e-2,lb], [z_max,z_max],'color','k','LineWidth',2,'linestyle','--')
end
end
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% change color map to have more change over smaller range from center
% cmap = jet(cmx-cmn+1);
% nc = 100; % number of colors
% a = 25; % number to set miniumu color
% b = 75;
% rcc = nc/(b-a); % rate of color change ie slope
% cmap = jet(nc);
% mxc = cmap(nc,:); % max color
% mnc = cmap(1,:); % minimum color
% cmap(1,:) = [1 1 1];
% cmap(2:a-1,:) = ones(a-2,1)*mnc;
% cmap(a:b,:) = jet(nc -(b-a)+1);
% cmap(b+1:nc,:) = ones(nc-b,1)*mxc;
% % cmap = jet(c*(cmx-cmn+1) + b);
% % cmap = [ones(1,3); cmap];
% colormap(cmap)
% colorbar
cb = colorbar('Location','SouthOutside','FontSize',FS);%'Position',[0.12 0.125 0.8 0.0125]);
xlabel(cb,'Spectrum Level [dB re 1\muPa^2/Hz]')
% 'HorizontalAlignment','center','Units','normalized','FontSize',FS)
set(cb,'XLim',[cmn+1 cmx])
% add plus sign to last label on colorbar
if 1
xtl = get(cb,'XTickLabel');
xtl = [char(xtl) blanks(length(xtl))'];
xtl(end,end) = '+';
set(cb,'XTickLabel',xtl)
end
% use path as title - could be better...
warning('off')
hT = title(fn_pathname,'FontSize',FS2);
set(hT,'Position',get(hT,'Position')+[0,lb*.06+3000*ptype,0])
%%
% Save plot to file
if spflag
opath = fn_pathname;
ofile = 'SingleLSG.jpg';
print('-f750','-djpeg','-r600',fullfile(opath,ofile))
ofile2 = 'SingleLSG.tif';
print('-f750','-dtiff','-r600',fullfile(opath,ofile2))
ofile3 = 'SingleLSG.fig';
saveas(gca,fullfile(opath,ofile3))
end