Reputation: 43
I used the spectrogram
function in MATLAB to generate spectrogram and save it as a mat file directly. Then, I used the same wav file and the same way to generate another spectrogram. But I saved it as a jpg file first and the saved the jpg file as a mat file. I am confused as to why the matrix sizes in the two mat files are different,the first one is 147*257
double and the second one is 256*256*3
uint8.
%% the first one
[x,fs] = audioread('1.wav')
spectrogram(x, window, L, N, fs);
set(gcf, 'position', [500,500,205,205]);
set(gca, 'Position', [0 0 1 1]);
f = getframe(gcf);
mat = getimage(gcf);
save(['D:\matlab\speech\', strcat(int2str(i)), '.mat'], 'mat', '-v6');
%% the second one
[x,fs] = audioread('1.wav')
spectrogram(x, window, L, N, fs);
set(gcf,'position', [500,500,205,205]);
set(gca,'Position', [0 0 1 1]);
f = getframe(gcf);
mat = getimage(gcf);
imwrite(f.cdata, ['D:\matlab\speech\', int2str(i),'.jpg']);
img_A = imread(fullfile(file_path, strcat(int2str(i), '.jpg')))
save(['D:\matlab\speech\',strcat(int2str(i)), '.mat'], 'img_A', '-v6');
Does anyone know why this happens? How can I properly store spectrogram as mat file?
Upvotes: 1
Views: 318
Reputation: 32094
f.cdata
matrix is larger because it includes the margins of the figure.
According to getframe
documentation:
getframe
Capture axes or figure as movie frameF = getframe(fig) captures the figure identified by fig. Specify a figure if you want to capture the entire interior of the figure window, including the axes title, labels, and tick marks. The captured movie frame does not include the figure menu and tool bars.
Example:
I = imread('peppers.png'); %Matrix size of I: 384 x 512 x 3
I = im2double(I); %Convert I from uint8 to double (result pixel range is [0, 1]).
figure;
imshow(I);
f = getframe(gcf);
mat = getimage(gcf); %Matrix size of mat: 384 x 512 x 3 and class(mat) is double.
fprintf('size(mat) = %d x %d x %d, class(mat) = %s\n', size(mat), class(mat)); % 384 x 512 x 3, class is double
fprintf('size(f.cdata) = %d x %d x %d, class(f.cdata) = %s\n', size(f.cdata), class(f.cdata)); % 479 x 664 x 3 (include margins), class is uint8.
figure;
imshow(f.cdata);
imwrite(mat, 'mat.jpg'); % 384 x 512 x 3 (double)
imwrite(f.cdata, 'f_cdata.jpg'); % 479 x 664 x 3 (uint8)
As you can see, size of f.cdata
is larger than mat
because of the margins of the figure.
In your case, mat
spectrogram image data type is double
and f.cdata
("frame image") type is uint8
.
Upvotes: 1