-
Notifications
You must be signed in to change notification settings - Fork 2
/
Spect_Generate_time.py
53 lines (43 loc) · 1.98 KB
/
Spect_Generate_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import matplotlib.pyplot as plt
import matplotlib
from scipy import signal
from scipy.io import wavfile
from dat_extract.extract.Ship_Variable_Extraction import Ship
from scipy import interpolate
import numpy as np
import numpy.matlib as npmb
import dat_extract.get_tf as tf
import math
import unpickle as up
import sys
import os
rootdir = 'D:\Pickled_Data_2\\' #set dir for the extraction
destination_folder = 'D:\Generated Spectrograms'
def generate(rootdir):
print('here')
ships = up.unpickle_ships(rootdir)
i = len(ships)-1
j = 0
while i > 0:
ship = ships[i]
try:
wavfilepath = os.path.join(ship.filepath , ship.id + '.wav') #the original wav file
print(wavfilepath)
#destination = destination_folder + ship.month +'\\' + ship.id + '.png' #the destination for the spectrogram
sample_rate, samples = wavfile.read(wavfilepath) #get original wav file samples at the original sample rate
frequencies, times, spectrogram = signal.spectrogram(samples,sample_rate, window = np.hanning(1024), noverlap = 0, nfft = 1024, mode='psd') #generate spectrogram
uppc = tf.get_tf(ship.harp,frequencies) #get the transfer function results
spectrogram = 10*np.log10(spectrogram) #convert to/from decibels ?
uppc = npmb.repmat(uppc,np.size(spectrogram,1),1) #copy tf results several times to make it same size as spect results
ship.spect = spectrogram + np.transpose(uppc) #add tf results to spect results
except:
j+=1
print("bad ship " + str(j))
print(sys.exc_info()[0])
up.one_jar(rootdir,ship,True)
pass
up.one_jar(rootdir,ship,False)
ships.pop(i)
print(i)
i-=1
generate(rootdir)