xticklabel in the midle of the spectrogram
5 views (last 30 days)
Show older comments
Hello all,
I have several .wav files with 3minutes each that I used to compute a spectrogram.
Now I want to put the name of each .WAV file in the xticklabels in the midle of the correspondent part of the espectrogram.
Does anyone have an idea how this can be done?
Thank you in advance.
0 Comments
Answers (1)
Voss
on 4 Oct 2023
Something like this maybe:
% some plot
plot(rand(1,1000));
% some file names:
name = {'a wav file.wav','another wav file.wav'};
% some region boundaries along the x-axis
x = [0 400 1000];
xline(x(2:end-1),'r','LineWidth',2); % so you can see the boundary/ies
% set xticks in the middle of each region, labeled according to 'name'
set(gca(),'XTick',(x(1:end-1)+x(2:end))/2,'XTickLabel',name);
5 Comments
Voss
on 5 Oct 2023
Edited: Voss
on 5 Oct 2023
You can keep track of the size of each wav data set (the number of samples) when you read each file. That is the row vector n_samples below. Then the boundaries are x = cumsum([0 n_samples]/Fs);
Si=-174;
concatenatedAudio = [];
tic;
n_files = numel(fileList);
n_samples = zeros(1,n_files);
%Loop through each audio file
for i = 1:n_files
%Load the audio file
[audio, Fs] = audioread(fileList(i).name);
% keep track of the size of each data set:
n_samples(i) = size(audio,1);
allDates{i} = fileList(i).date;
%Concatenate the audio data
concatenatedAudio = [concatenatedAudio; audio];
end
concatenatedAudio=single(concatenatedAudio);
%% Spectrogram calculation
% Example spectrogram parameters
if strcmp(windowType,'Hann')
window = (0.5 - 0.5*cos(2*pi*(1:Fs)/Fs));%hann
elseif strcmp(windowType,'Hamming')
window = (0.54 - 0.46*cos(2*pi*(1:Fs)/Fs)); %hamming
end
[S, F, T, P] = spectrogram(concatenatedAudio, window, [], [lowfreq:Fs/2] ,Fs);
% Plot the spectrogram
PdB=10*log10(abs(P'))-Si;
figure; surf(T,F,PdB'); view(2); shading flat;set(gca,'YScale','log')
% set the xticks and xticklabels
x = cumsum([0 n_samples]/Fs);
set(gca(),'XTick',(x(1:end-1)+x(2:end))/2,'XTickLabel',{fileList.name});
colormap('parula'); % Choose a colormap
title('Spectrogram of Combined Audio Files');
xlabel('Time (s)');
ylabel('Frequency (Hz)');
colorbar
ax = gca; % axes handle
ax.YAxis.Exponent = 0;
xlim([T(1) T(end)]);
ylim([F(1) F(end)]);
caxis([0 max(max(PdB))]);
ylabel(colorbar,['PSD [ dB re ' num2str(1) ' \muP'...
'a^2 Hz^-^1 ]'],'fontname','arial','fontsize',14);
PSDTime=toc;
fprintf('PSD time elapsted: %s',num2str(PSDTime))
fprintf('s','\n')
fprintf('\n')
See Also
Categories
Find more on Simulation, Tuning, and Visualization in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!