Data, which isn't missing, appears to be missing in surf plot
Show older comments
Can anyone explain the gaps (white bars) which arise in my surf plot? The degree to which they appear changes depending on whether I plot the spectrogram as a full figure or a subplot... I would like to remedy this so there are no white bars in the subplot of the spectrogram.

subplot(3,1,3)
surf(t,f,A','FaceColor','interp',...
'EdgeColor','none','FaceLighting','phong');
set(gca,'tickdir','out','layer','top','fontname',... %change size of axes
'arial','fontsize',8);
ylim([50 2000]);
axis tight
view(0,90);
grid off
set(gca,'YScale','log'); %set y-axis scale to logarithmic
datetick('x','keepticks'); %change x axis to readable time
ylabel('Frequency ( Hz )','fontsize',14);
13 Comments
Walter Roberson
on 7 Oct 2021
set(gca,'YScale','log'); %set y-axis scale to logarithmic
I predict that you have negative A values.
Louise Wilson
on 7 Oct 2021
Edited: Louise Wilson
on 7 Oct 2021
Walter Roberson
on 7 Oct 2021
Please attach a .mat with t, f, A arrays for us to test with.
Mathieu NOE
on 7 Oct 2021
hello Louise
does this post has a relatioship with tthis one ?
sems there that in the other post you display the log of P which is positive real valued array, so there should not be any need to do such test : [row col]=find(A<0)
This code (other post ) does not generate any blanks BTW... :
%% read multiple files
fileDir = pwd; % currrent directory
fileNames = dir(fullfile(fileDir,'*.wav')); % get list of files in directory
fileNames_sorted = natsortfiles({fileNames.name}); % sort file names into order (https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
nFiles = numel(fileNames_sorted);
% my own paramters value here
cal = 1;
nfft = 1024;
window = hanning(nfft);
overlap = 512;
Tstart = zeros(1,nFiles);
for iFile = 1:nFiles
fullfname = fullfile(fileDir, fileNames_sorted{iFile});
t0 = Tstart(iFile);
% [xbit,fs]=audioread(fullfname, [nlo nup]); %read in wav
[xbit,fs]=audioread(fullfname); %read in wav
xbit=detrend(xbit); %remove DC offset
calxbit=xbit*cal; %apply calibration
%Perform FFT and plot:
[S,F,T,P] = spectrogram(calxbit,window,overlap,nfft,fs,'yaxis');
sph(iFile) = subplot(nFiles,1,iFile);
h = pcolor(T,F,10*log10(P)); % Here you might want to add the start-time, t0, to T
% with proper conversion of time-format...
set(h,'EdgeColor','none');
xlabel('Time (s)');
ylabel('Frequency (Hz)');
end
test result :

Louise Wilson
on 7 Oct 2021
Louise Wilson
on 7 Oct 2021
Edited: Louise Wilson
on 7 Oct 2021
Mathieu NOE
on 8 Oct 2021
hello Louise
could you confirm how your wav file names are labelled ? I created some dummy ones on my side but I have some trouble with extracting the usefull time information from the filenames
my file name is like : test.20210202151315.cut.wav (from
%format is xxxx.yyMMddHHmmss.cut.wav)
so far I understand the only info we need to plot is the seconds extracted from the file name ? and this will be the start time on each x axis
Louise Wilson
on 8 Oct 2021
Edited: Louise Wilson
on 8 Oct 2021
Mathieu NOE
on 11 Oct 2021
hello Louise
ok - i am getting closer to the solution now . Still have to figure out how o keep only the first X Tick
for the time being I stll have all seconds displayed
here we can see the X tick label is consistent with the filename (seconds)
my dummy wav files names are :
test.210201111815.cut.wav
test.210304161825.cut.wav
test.210407094535.cut.wav
so the first plot should have a X Tick starting with 15 s , the second plot 25 s , and the third one 35 s

%%%%%%%%%%%%%%%%%%%%%%%%
% directory={'X:\SoundTrap\Boats\wav\Noises\LTSA test\LTSA test batch'}; %folder where wav files are
% folder=char(directory);
folder= pwd;
fullfnames=dir(fullfile(folder,'*.wav')); %list all .wavs in folder
nFiles=numel(fullfnames);
%get fs of first (i.e. all) files in folder
firstfile=fullfile(folder,char(fullfnames(1,1).name));
[xbit,fs]=audioread(firstfile,[1 2]);
tlo=1.0;
% thi=114.0;
thi=3.0; % for my demo
nlo=fs*tlo; %multiply fs by tlo to get starting point
nup=fs*thi; %do the same for end point
nfft=1024; % for my demo
% nfft=16384;
overlap=nfft/2; %50% overlap
% window=16384;
window=hanning(nfft);
cal=176.4;
cal=power(10,cal/20); %calculate calibration value
t0=0;
dt=1;
for iFile = 1:3 %nFiles
fullfname = fullfnames(iFile).name;
%format is xxxx.yyMMddHHmmss.cut.wav
% you'll have to set/get/extract start-time for each spectrogram
fname=strsplit(fullfname,{'.'});
fname_st=fname(2);
fname_st=datenum(fname_st,'yyMMddHHmmss');
tStartstrings{iFile} = fname_st;% you'll have to set/get/extract start-time for each spectrogram
[xbit,fs]=audioread(fullfile(folder,fullfname), [nlo nup]); %read in wav
xbit=detrend(xbit); %remove DC offset
calxbit=xbit*cal; %apply calibration
%Perform FFT and plot:
[S,F,T,P] = spectrogram(calxbit,window,overlap,nfft,fs,'yaxis');
T=fname_st+datenum(seconds(T)); %start time of .wav file + seconds of each chunk to produce informative x-axis
sph(iFile) = subplot(1,nFiles,iFile);
h = pcolor(T,F,10*log10(P)); % Here you might want to add the start-time, t0, to T
hold on
set(h,'EdgeColor','none');
datetick('x','SS')
xlabel(fullfname(1:length(fullfname)-4));
ylabel('Frequency (Hz)');
end
Mathieu NOE
on 12 Oct 2021
hello Louise
so how are you today and what still do we need to do to reach the finish line ?
all the best
Louise Wilson
on 12 Oct 2021
Louise Wilson
on 19 Oct 2021
Mathieu NOE
on 25 Oct 2021
Hello Louise
this is a result with all spectrograms in one plot - the data are separated by zeros just to avoid any misinterpretation of the plot
of course the wav files should be sampled at the same sampling freq - but even in a different scenario we can find a fix fr that
of course I still need to figure out how to make the x axis labelling working - that is still work ahead

code so far :
% %% read multiple files
% fileDir = pwd; % currrent directory
%
% fileNames = dir(fullfile(fileDir,'*.wav')); % get list of files in directory
% fileNames_sorted = natsortfiles({fileNames.name}); % sort file names into order (https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
% nFiles = numel(fileNames_sorted);
%
clc
clearvars
close all
%%%%%%%%%%%%%%%%%%%%%%%%
% directory={'X:\SoundTrap\Boats\wav\Noises\LTSA test\LTSA test batch'}; %folder where wav files are
% folder=char(directory);
folder= pwd;
fullfnames=dir(fullfile(folder,'*.wav')); %list all .wavs in folder
nFiles=numel(fullfnames);
%get fs of first (i.e. all) files in folder
firstfile=fullfile(folder,char(fullfnames(1,1).name));
[xbit,fs]=audioread(firstfile,[1 2]);
tlo=1.0;
% thi=114.0;
thi=3.0; % for my demo
nlo=fs*tlo; %multiply fs by tlo to get starting point
nup=fs*thi; %do the same for end point
nfft=1024; % for my demo
% nfft=16384;
overlap=nfft/2; %50% overlap
% window=16384;
window=hanning(nfft);
cal=176.4;
cal=power(10,cal/20); %calculate calibration value
t0=0;
dt=1;
P_all = [];
for iFile = 1:3 %nFiles
fullfname = fullfnames(iFile).name;
%format is xxxx.yyMMddHHmmss.cut.wav
% you'll have to set/get/extract start-time for each spectrogram
fname=strsplit(fullfname,{'.'});
fname_st=fname(2);
fname_st=datenum(fname_st,'yyMMddHHmmss');
tStartstrings{iFile} = fname_st;% you'll have to set/get/extract start-time for each spectrogram
[xbit,fs]=audioread(fullfile(folder,fullfname), [nlo nup]); %read in wav
xbit=detrend(xbit); %remove DC offset
calxbit=xbit*cal; %apply calibration
%Perform FFT and plot:
[S,F,T,P] = spectrogram(calxbit,window,overlap,nfft,fs,'yaxis');
P_all = [P_all P zeros(length(F),25)]; % concatenate all P data with some blanks in between
T=fname_st+datenum(seconds(T)); %start time of .wav file + seconds of each chunk to produce informative x-axis
end
[a,samples] = size(P_all);
t = (0:samples-1)*1/fs;
h = pcolor(t,F,10*log10(P_all)); % Here you might want to add the start-time, t0, to T
colormap(jet);
set(h,'EdgeColor','none');
datetick('x','SS')
title(fullfname(1:length(fullfname)-4));
xlabel('Time');
ylabel('Frequency (Hz)');
Answers (0)
Categories
Find more on Time-Frequency Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
