Half plotting problem of spectrogram

10 views (last 30 days)
I am producing spectrograms of 10 hour audio in chunks of 4 seconds, by using the "for" loop and storing it in a folder. But many of the spectrograms generated and saved are incomplete. Why it is so? and how I can stop this to happen
This problem is not for all spectrograms, only for 30% of the total.
  2 Comments
Sania Gul
Sania Gul on 27 Oct 2022
Here is my code :)
clear all;
clc;
close all;
Fs=16000;
fs=Fs;
audio=[];
opening=0;
count =1;
% Load the source file
while 1
samples = [1+opening,count*Fs*4.12];% 16 time frames corresponds to 0.287 seconds interval
if samples(2)>540938091% total sample in audio train file
break
end
% if samples(2)>33152737% total sample in audio test file
% break
% end
audio=audioread('D:\Fazeel_Data\16KAll\TrainNoisy.wav',samples);
%Destination folder for storing images
Dest = 'D:\Fazeel_Data\Parula\TrainNoisyeePicsDup\';
% figure(1);
% spectrogram(audio,hann(512),256,512,Fs,'yaxis');
[Q,F,T] = spectrogram(audio,hann(512),256,512,Fs,'yaxis');
% Taking the log of Z-Axis (Brightness)
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',[])
% print('AB','-djpeg','-r50');
% I=imread('AB.jpg');
% [a b c]=size(I)
export_fig AA.jpg -native -c[31 52 46 72]% top right bottom left
% resizing the image
I=imread('AA.jpg');
outputImage = imresize(I, [256,256]);
% imshow(outputImage);
export_fig AA.jpg -native -c[31 86 58 85]% top right bottom left
% imshow(outputImage);
% Copying the figure from current directory to Destination folder
% psource = 'D:\Fazeel_Data';
% pattern = 'AA.jpg';
% sourceFile = fullfile(psource, pattern);
[o p q]=size(outputImage)
imagefilename = sprintf('TrainNoisyee_%05d.jpg',count);
destFile = fullfile(Dest, imagefilename); % Or another name?
% copyfile(sourceFile, destFile);
imwrite(outputImage, destFile);
opening=samples(2);
count=count+1;
% pause(1);
end
end

Sign in to comment.

Accepted Answer

VBBV
VBBV on 7 Nov 2022
Edited: VBBV on 7 Nov 2022
imwrite(outputImage, destFile,'Quality',"lossless");
It appears that some part of the image data is being lost during write operation. Try with name value argument specifying quality as lossless
  6 Comments
VBBV
VBBV on 8 Nov 2022
Edited: VBBV on 8 Nov 2022
I = imread('peppers.png');
imshow(I)
[r c] = size(I) % get the size of image
r = 384
c = 1536
if r == c
outputImage = imresize(I,[r c]); % this could be reason, change using scale value
elseif r < c
outputImage = imresize(I,[r+abs(c-r) c]); ;% this could be reason, change using scale value
elseif r > c
outputImage = imresize(I,[r c+abs(c-r)]);
end
outputImage = imresize(I,[120 c]); % see the difference with fixed row and col values
imshow(outputImage)
% imwrite(outputImage, destFile,"Quality",100); % switch back to normal mode
Note the difference when you use fixed numbers for rows and cols for all image sizes, Try with the higher or scaled values for rows and cols inside the imresize function and switch back to normal mode. May be some images have high resolution with large or different sizes,
Sania Gul
Sania Gul on 9 Nov 2022
Tnk u VBBV. The photo is compressed, however complete in your case. However, it is half drawn or incomplete in my case. I have to be restricted in defining the image size as my spectrograms are given as input to pix2pix network. Secondly I have generated 8000 spectrograms by the same set of instructions and only random 30% suffer from this problem. This may seems to be some issue in memory allocation by the processor as C drive is almost full. But anyways thanks for your time and efforts. I regard them high :-)

Sign in to comment.

More 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!