Align time axes of two plots?

6 views (last 30 days)
Louise Wilson
Louise Wilson on 19 Oct 2020
Edited: Louise Wilson on 20 Oct 2020
I have two figures where the x-axis is time, and they overlap in time. One is a spectrogram of a sound file and the other shows the distance of a vessel from where the sound file was recorded. The x-axis overlap in time but I am wondering how I could sync them up accurately? I have adjusted the time axes so they line up in time but visually they are off slighty. I have tried removing the colourbar but that doesn't help, plus I would like to keep the colourbar in. Also, how could I plot two spectrograms (two .wav files) side by side?
%Input variables
site=3; %site working on
wavfolder=('Y:\SoundTrap\Boats\wav\Kawau\006_Kawau_5278'); %wav file folder
wavfile=('5278.191210100314.wav'); %file working on
hydrophonesites=('Y:\AIS data\CPA_FILT\hydro_locations.xlsx'); %hydrophone sites
gpsfolder=('Y:\HOLUX GPS data'); %gps folder
gpsfile=('20191210_GPS_Koura.csv'); %gps file
%Get hydrophone sites
PAMlocs = xlsread(hydrophonesites,'A2:B11');
[~,PAMlocname] = xlsread(hydrophonesites,'C2:C11');
[~,PAMloccode] = xlsread(hydrophonesites,'E2:E11');
disp(PAMlocname{site})
PAMlat = PAMlocs(site,1);
PAMlon = PAMlocs(site,2);
%Get GPS data
opts=detectImportOptions(fullfile(gpsfolder,gpsfile));
opts.SelectedVariableNames={'LOCALDATE','LOCALTIME','LAT','LONGITUDE'}; %only read variables of interest
opts=setvaropts(opts,'LOCALDATE','InputFormat','dd/MM/yyyy'); %read date in correct format
gps=readtable(fullfile(gpsfolder,gpsfile),opts); %read file
gps.LOCALDATE=gps.LOCALDATE+gps.LOCALTIME; %add time to datetime
gps.LOCALDATE.Format='dd-MM-yyyy HH:mm:ss';
gps.LOCALDATE=datenum(gps.LOCALDATE);
gps = removevars(gps, 'LOCALTIME');
gps.LOCALDATE=datenum(gps.LOCALDATE);
gps=table2array(gps);
%Calculate distances between boat and hydrophone
gps(:,4)=deg2km(distance(gps(:,2),gps(:,3),PAMlat,PAMlon));
%Plot spectrogram
subplot(2,1,2)
wavfilename=fullfile(wavfolder, wavfile);
%Input variables for audioread:
fs=144000;
tlo=1.0; %select only part of data and so remove cal.tone
thi=299.0;
nlo=fs*tlo; %multiply fs by tlo to get starting point
nup=fs*thi; %do the same for end point
dur=0.5;
nfft=window;
window=round(fs*dur);
overlap=window/2; %50% overlap
%Get calibration info:
SN=strsplit(wavfile, {'.','\'});
SN1=char(SN(1));
serialNo=str2num(SN1);
switch serialNo
case 5278
cal=175.9;
otherwise error('Unknown ST serial');
end
cal=power(10,cal/20); %calculate calibration value
[xbit,fs]=audioread(fullfile(wavfolder,wavfile), [nlo nup]); %read in wav
xbit=detrend(xbit); %remove DC offset
calxbit=xbit*cal; %apply calibration
[cal,F,T,P] = spectrogram(calxbit,window,overlap,nfft,fs,'yaxis'); %perform FFT and plot
h = pcolor(T,F,10*log10(P));
set(h,'EdgeColor','none');
subplot(2,1,2)
set(gca,'tickdir','out','layer','top','fontname',... %change size of axes
'arial','fontsize',12);
colormap(gca,parula); %change colour scheme of plot
%Y axis
ylabel('Frequency (Hz)','FontSize',18,'FontName','Arial'); %label
ylim([50 5000]); %sets the y -axis frequency limits
set(gca, 'YScale', 'log'); %set y-axis as lin or log
%X axis
xlabel('Time (seconds)','FontSize',18,'FontName','Arial'); %label
xlim([1,299]); %change range of x-axis
%xticks(2:20:299); %x axis labels in intervals of 15
%Colourbar
y=ylabel(colorbar,['PSD (dB re 1 uPa^2 Hz^-^1)'],'fontname','arial',...
'fontsize',18, 'Rotation', 270, 'Position',...
[4.32857152393886 82.8072636129137 0]); %add in PSD colour bar
caxis([40 120]); % sets scale on colour bar
%Get dates for x axis
fileparts=strsplit(wavfile,'.');
filetime=fileparts(2)
filetime=datetime(filetime,'InputFormat','yyMMddHHmmss')
starttime=filetime+seconds(tlo)
endtime=filetime+seconds(thi)
datevector=starttime:seconds(1):endtime; %times of .wav file in 1s interval
%find gps times which overlap with .wav file times
[matchingtimes,Locb]=ismember(datetime(gps(:,1),'ConvertFrom','datenum'),datevector);
gps_sub=gps(matchingtimes,:); %subset gps times
%find position of gps times in .wav file times
[Lia,Locb]=ismember(datevector,datetime(gps_sub(:,1),'ConvertFrom','datenum'));
datevector_sub=datevector(Lia); %subset wav file datetimes to match gps times
%where in datevector does first value of datevector_sub occur?
%[Lia,Locb]=ismember(datevector_sub,datevector);
%firstvalue=Locb(1)
%xticks(firstvalue:20:299);
%times=datevector(firstvalue):seconds(20):endtime;
%OR
xticks(1:20:299);
times=datevector(1):seconds(20):datevector(299);
xlabels=cellstr(datestr(times));
xticklabels(xlabels);
xtickangle(45);
%Plot GPS
hold on
subplot(2,1,2)
plot(datetime(gps_sub(:,1),'ConvertFrom','datenum'),gps_sub(:,4),'LineWidth',2,'Color',[1 0 0])
xticks(starttime:seconds(20):endtime); %change range of x-axis using times from .wav
xlim([starttime, endtime]);
xtickangle(45);
ylabel('Distance (km) of vessel to hydrophone');
%We want 'starttime' to be the first value in all plots, but 'firsttime' is
%the first labelled time.
%gps_sub_dt=datetime(gps_sub(:,1),'ConvertFrom','datenum');
timebefore=(firsttime-seconds(6)):seconds(1):firsttime-seconds(1);
check=ismember(datetime(gps(:,1),'ConvertFrom','datenum'),timebefore);
earliertime=gps(check,:);
gps_sub=[earliertime; gps_sub]; %extend start time of gps data to meet with .wav file contents
set(colorbar,'visible','off'); %align plots

Answers (0)

Categories

Find more on Polar Plots in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!