1

I wrote a simple program to convert .wav to spectogram and save this as an png. Here you go:

import numpy as np
import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile
import os
import time as t

DATAPATH = 'dataset' #path
CATEGORIES = ['zero','one','two','three','four','five','six','seven','eight','nine']

for categorie in CATEGORIES:
    path = DATAPATH + '/' + categorie + '/'
    filenames = os.listdir(path) #get all filenames in categorie
    print(categorie)

    i = 0
    for file in filenames[:100]:
        start = t.time()
        Fs, aud = wavfile.read(path + file)
        powerSpectrum, frequenciesFound, time, imageAxis = plt.specgram(aud, Fs=Fs)

        plt.subplots_adjust(left=0, right=1, bottom=0, top=1) #cut axis
        plt.axis('off')

        plt.savefig('pics/' + categorie + '/' + str(i) + '.png')
        ende = t.time()
        print(i, str(ende-start)+'s')
        i += 1

The problem is that the time per image getiing higher and higher (only for a few milisekonds) but at the thousand pic it will be like 10sek per pic. Thats why I stopp the time and print it out. Some solutions?

The Reider
  • 143
  • 6
  • 1
    Your output plots look how they should? It sounds like the plot is accumulating stuff instead of flushing after every iteration. Does this question help? [matplotlib.pyplot will not forget previous plots - how can I flush/refresh?](https://stackoverflow.com/questions/17106288/matplotlib-pyplot-will-not-forget-previous-plots-how-can-i-flush-refresh) – aaossa Feb 21 '22 at 11:30
  • 1
    @aaossa a lot of thanks you helped me alot and now my code works how it should – The Reider Feb 24 '22 at 19:11

1 Answers1

0

FTR, the solution seems to be cleaning the plot after every iteration using plt.clf():

for categorie in CATEGORIES:
    # ...
    for file in filenames[:100]:
        # ...
        # plt.savefigs(...)
        plt.clf()
        # ...
aaossa
  • 3,763
  • 2
  • 21
  • 34