Plotting matrix as spectrogram

I want to plot a matrix of 256* 16, as spectrogram. But when I use surf command to do so, I got spectrogram of size 343*436. I want the image to be of same size as that of matrix i.e. 256*16. Kindly suggest any solution without cropping or resizing the image as both of them distorts the quality.

3 Comments

How a matrix of 256*16 changed to 343*436? You need to show us your code.
testData = randn(256,16);
fh = figure;
ah = axes(fh);
surf(ah,testData);
I was not able to reproduce the described error by the given information.
Kind regards,
Robert
[Q,F,T] = spectrogram(audio,hann(512),256,512,Fs,'yaxis');
Q=Q(1:256,:);
F=F(1:256);
figure(2)
D=surf(T,F,log(abs(Q).^2));
colormap parula
shading interp
view([0 90])
axis tight
set(gca,'xtick',[])
set(gca,'ytick',[])
export_fig AA.jpg -native -c[31 52 46 72]% top right bottom left
% reading the image
I=imread('AA.jpg');
>>The value of I=343x436x3 uint8, but the size of matrix Q was only 256x16, and I want 'I' to be also 256x16x3 uint8

Sign in to comment.

 Accepted Answer

Hi Sania Gul,
The values you provide are not consistent. I assume your "audio" is a vector of a certain length N, sampled with the frequency Fs.
Applying the function spectrogram with the window-length 512, overlap 256 and NFFT of 512 lead certainly to the dimension of Q of NFFT/2+1 = 257 and ((N - 512)/(512-256)).
If you want Q to be 257 x 16 you would have to provide a snippet of roughly 4600 samples.
fs = 60e3;
t = 0:1/fs:4599/fs;
x1 = vco(sawtooth(2*pi*t,0.5),[0.1 0.4]*fs,fs);
[Q,F,T] = spectrogram(x1,hann(512),256,512,fs,'yaxis');
size(Q)
ans = 1×2
257 16
Kind regards,
Robert

4 Comments

You are right Robert. But this is not what I wanted.
I have to store this spectrogram as an image (without frames, axis and borders), and for that I have used export_fig function from Yair Altman, and stored it as 'AA .jpg' image in the current directory.
Now, when I retrieved it back and checked the image size of 'AA.jpg', it was 343x436x3 uint8, which is clearly not equal to my original matrix Q.
From documentation of your linked export_fig:
This function saves a figure or single axes to one or more vector and/or bitmap file formats, and/or outputs a rasterized version to the workspace, with the following features:
  • Figure/axes reproduced as it appears on screen
I guess, on screen the figure is shown as 343x436 while filling additional pixels for appearance. If you want to save the figure with original pixel count use print(). You would have to recalculate resolution though.
Kind regards,
Robert
Tnk u soooo much, but how to get rid of the frame around the spectrogram without using the export.fig?
There is still some manual work required to get the desired result.

Sign in to comment.

More Answers (0)

Asked:

on 26 Aug 2022

Commented:

on 26 Aug 2022

Community Treasure Hunt

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

Start Hunting!