How to combine multiple spectrogram and create a single image in MATLAB
Show older comments
Hello I hope you are doing well. I have the 3 vectors. I want to create a spectrogram of each vectors first save the spectrogram.
and combine the three vectors to create a single overlap spectrogram and save as image file
How can i do it in MATLAB
Answers (1)
Walter Roberson
on 12 Sep 2022
When you permit spectrogram() to display the result, then it always clears the axes it is drawing in. spectrogram() does not just display the result: the output has active controls.
Because of this, if you let spectrogram() display the output, you cannot merge the results at all easily.
What you can do is ask for outputs from spectrogram(), and use the outputs to draw the results any way that is appropriate for you.
But perhaps I am misunderstanding you. Perhaps what you want to do is something more like
combined_signal = mean([first_signal(:), second_signal(:), third_signal(:)], 2);
This assumes that they are all the same length; if not then you will need to pad the shorter ones first.
13 Comments
Stephen john
on 12 Sep 2022
Stephen john
on 13 Sep 2022
Walter Roberson
on 13 Sep 2022
remean = mean([re1; re2; re3]);
signals = {re1, re2, re3, remean};
signalnames = {'re1', 're2', 're3', 'mean'};
numsigs = length(signals);
[Scell, Fcell, Tcell, Pcell] = cellfun(@spectrogram, signals, 'uniform', 0);
tiledlayout
for K = 1 : numsigs
nexttile();
scaled_data = rescale(10*log10(Pcell{K}.');
image(Fcell{K}, Tcell{K}, scaled_data));
title(signalnames{K});
end
Inside the for K loop, scaled_data is data in the range 0 to 1 that you could imwrite(). scaled_data in itself does not have the axis labels -- so you would get just the color part of the spectrogram
I did not bother following the code details to get the axis labels right; and it looks like I might have missed a detail on getting the colors right.
Stephen john
on 13 Sep 2022
Stephen john
on 13 Sep 2022
Walter Roberson
on 13 Sep 2022
Sorry, I thought I posted the corrected version.
load Spectogramdata.mat
remean = mean([re1; re2; re3]);
signals = {re1, re2, re3, remean};
signalnames = {'re1', 're2', 're3', 'mean'};
numsigs = length(signals);
[Scell, Fcell, Tcell, Pcell] = cellfun(@spectrogram, signals, 'uniform', 0);
tiledlayout('flow')
for K = 1 : numsigs
nexttile();
imagesc(Fcell{K}, Tcell{K}, 10*log10(Pcell{K}.'));
title(signalnames{K});
end
I want to stich the spectogram on on another to get a single spectrogram
I will need an example of what you want the result to look like. Earlier you asked to "combine" the three signals, which I did by taking the mean of them, as if all three signals were playing simultaneously and you needed to run a spectrogram of the combination.
Siriki Kone
on 24 Sep 2022
Where did you define the axis values of your resulting spectrogram? or how would you define the x and y axis since you combined 3 differents plots?
Walter Roberson
on 24 Sep 2022
I am not sure who you are addressing, Siriki Kone?
In what I posted, the last spectrogram is the over the mean of the signals and has the same units and scales as the original signals (provided that they are consistent.)
But taking the mean does not appear to be what Stephen John wanted, and they did not post an example of what they were looking for so there isn't anything more I can do for them at this point. Maybe they were interested in stackedplot()
Siriki Kone
on 25 Sep 2022
Hi Mr. Robertson,
I think it would better to explain my problems with this spectrogram plot:
i have ground acceleration data on a hourly basis for a whole month. I want to plot the spectrogram of a whole month, so my idea was to plot a seismogram for every my hourly acceleration data and to merge them. I think what you proposed here could a way to do what i want..
im trying to plot the spectrogram of my hour data but i get an error notification, i thought you could tell what iam not doing right:
s_rate=200;
dt=1/s_rate;
nov = length(acc(:,n)); %my acc has the dimensions (262144,1)
section1 = ceil(nov/13); %window my acc data in 13 segments
window1 = hanning(section1); %apply a hanning fenster on it
noverlap1 =ceil(3*nov/4); %75% overlapping
f= [0 100]; %my frequencies go from 0 30-40 Hz so i choose to take max f intervall
[s,f,t,psd] =spectrogram(acc,section1,noverlap1,f,s_rate,'psd');
%i get the following errors:
% Error in pspectrogram (line 30)
% [xw,nx,~,yw,ny,win,~,~,noverlap,~,~,options] =... signal.internal.spectral.welchparse(x,esttype,inpArgs{:});
%Error in spectrogram (line 191)
% [varargout{1:nargout}] = pspectrogram({x},'spect',inpArgs{:});
% Error in Spectrog_calcul_test (line 157)
% [s1,t1,f1,psd1] = spectrogram(acc,section1,noverlap1,f,s_rate,'psd');
could you or someone help with that? how could i graduate the x axis (if my code works) so that my x axis would begin from from example 00:00:00 to 01:00:00 at a specific date? is it possible to have a specfic text on the x axis every 2dt for example?
Thanks for your help!
Siriki Kone
on 25 Sep 2022
my signal has the dimensions acc (262144,1)
Walter Roberson
on 25 Sep 2022
nov = length(acc(:,n)); %my acc has the dimensions (262144,1)
unless n = 1 and acc is a vector, acc would be 2d
[s,f,t,psd] =spectrogram(acc,section1,noverlap1,f,s_rate,'psd');
You are passing all of acc to the function, not just a vector slice.
If acc can only be vector then get rid of the n which is implying that acc is 2d. If acc is 2d then pass acc(:, n) to the function.
Siriki Kone
on 25 Sep 2022
Do you mean it would the cause of these errors? because was always n=1 but it doesnt change anything, i get the same errors.
Actually I first add my acc with dimensions (:,13) because i subdivised each data in 13 segments. I estimate then the power spectral density for each of them (PSD_Station=10*log10(acc(:,n)); the problem is that i don't know how to plot these psd as an image with time against frequency, i tried with surf, or imagesc but it didnt work.
any solution would be really helpful!
Walter Roberson
on 25 Sep 2022
s_rate=200;
dt=1/s_rate;
n = 1;
nov = length(acc(:,n)); %my acc has the dimensions (262144,1)
section1 = ceil(nov/13); %window my acc data in 13 segments
window1 = hanning(section1); %apply a hanning fenster on it
noverlap1 = ceil(3*section1/4); %75% overlapping
f= [0 100]; %my frequencies go from 0 30-40 Hz so i choose to take max f intervall
[s,f,t,psd] =spectrogram(acc,section1,noverlap1,f,s_rate,'psd');
Categories
Find more on Blocked Images 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!