1
votes

I'm attempting to generate spectrograms so that I could compare them to word samples later for a very basic speech recognition project. This is being done in a loop. The code is as follows:

folder = '<<my directory path>>';
files = dir(fullfile(folder,'*.wav'));
for k = 1:length(files)
    baseFileName = files(k).name;
    fullFileName = fullfile(folder, baseFileName);
    fprintf(1, 'Now reading %s\n', fullFileName);
    [word, fs] = wavread(fullFileName);
    sound(word, fs);
    figure('visible','off');
    %%fig = figure, specgram(fullFileName, 512, fs);
    %%saveas(fig, baseFileName.fig);
end

My issue is in the last two lines. I want to save the spectrogram figure as a .fig file (unless you guys have a better suggestion for comparison purposes later on) and name it the same as baseFileName. I've been googling for a while now but the formatting is confusing and there isn't much about spectrogram being saved as figures.

I would prefer it if all the figures didn't pop up in the loop but I can deal.

EDIT: perhaps it will be better to store this as a matfile?

1

1 Answers

0
votes

Does this work for you?

for k=1:length(files)
   h=figure
   spectrogram();
   Figname = strcat(baseFileName,'.fig');
   saveas(h,Figname); 
end

For the figures with the format other than .fig, another method:

fh=figure
for k=1:length(files)
   figure(fh); 
   spectrogram();
   frm = getframe(fh);
   Figname = strcat(baseFileName,'.jpg');
   imwrite(frm.cdata, Figname);
end

Unfortunately it will cause error 'Unable to determine the file format from the filename' when trying to save a .fig file with this method.