예제 #1
0
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
예제 #2
0
def plot(folder):
    ships = up.unpickle_a_batch(folder, 0, 1)
    for ship in ships:
        wavfilepath = ship.filepath + ship.id + '.wav'  #the original wav file
        sample_rate, samples = wavfile.read(
            wavfilepath
        )  #get original wav file samples at the original sample rate
        destination = destination_folder + ship.year_month + '\\' + ship.id + '.png'  #the destination for the spectrogram
        print(ship.id)

        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
        spectrogram = spectrogram + np.transpose(
            uppc)  #add tf results to spect results
        spectrogram = normalize(spectrogram)
        times = times[:512]
        plt.yscale('log')  #make y scale log to match the new decibel units
        axes = plt.gca()  #get axes object
        axes.set_ylim([10, 1000])  #set upper limit of data on axes to be 1000
        plt.pcolormesh(times, frequencies, spectrogram, vmin=60,
                       vmax=110)  #plot the data and add color
        plt.set_cmap('jet')
        plt.ylabel('Frequency [Hz]')
        plt.xlabel('Distance [km]')

        plt.colorbar()
        #plt.xticks(locs, new_ticks)  # Set locations and labels to the distance
        #plt.savefig(destination) #save spectrogram at destination
        #plt.imshow(spectrogram)
        plt.show()  #show plot
        plt.close()
예제 #3
0
def generate(folder):
    ships = up.unpickle_ships(folder)
    bad_apples = []
    for ship in ships:
        try:

            wavfilepath = ship.filepath + ship.id + '.wav'  #the original wav file
            txtfilepath = ship.filepath + ship.id + '.txt'  #the original txt file
            destination = destination_folder + ship.year_month + '\\' + ship.id + '.png'  #the destination for the spectrogram
            print(txtfilepath)

            converted_times, start, cpa_index, cpa_time = convert_time(
                ship
            )  #convert all times and find the file start time and cpa time
            #print(start)
            #print(converted_times)
            #print(cpa_time)
            #print(cpa_index)
            after_start = ship.distance[
                start:]  #find all distances after file_time
            # print(len(after_start))
            # print(after_start)
            closest_range = np.min(
                np.abs(after_start))  # find closest point of approach (cpa)
            cpa_index = after_start.index(closest_range)
            # print(cpa_index)
            pre_cpa = after_start[:cpa_index]
            post_cpa = after_start[
                cpa_index:]  #find all distances after cpa time
            # print(len(pre_cpa))

            pre_times = converted_times[:cpa_index]
            # print(len(pre_times))
            post_times = converted_times[cpa_index:]
            # print(post_times)
            #print(pre_cpa)
            #print(post_cpa)
            approach_inter = interpolate.interp1d(pre_times,
                                                  pre_cpa,
                                                  axis=0,
                                                  fill_value="extrapolate")
            depart_inter = interpolate.interp1d(post_times,
                                                post_cpa,
                                                axis=0,
                                                fill_value="extrapolate")

            sample_rate, samples = wavfile.read(
                wavfilepath
            )  #get original wav file samples at the original sample rate

            sound_length = len(samples) // sample_rate
            #print(sound_length)
            approach_times = np.arange(0, cpa_time)
            depart_times = np.arange(cpa_time, sound_length)

            frequencies, times, spectrogram = signal.spectrogram(
                samples,
                sample_rate,
                window=np.hanning(10e3),
                noverlap=0,
                nfft=10e3,
                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
            spectrogram = spectrogram + np.transpose(
                uppc)  #add tf results to spect results

            range_step = .01  # step size of 1m

            range_approach = (
                (np.arange(pre_cpa[0], closest_range, -range_step))
            )  # make a vector of distances between first range and cpa
            range_depart = (
                np.arange(closest_range, post_cpa[len(post_cpa) - 1],
                          range_step)
            )  # make a vector of distances between cpa and last range
            range_desired = np.append(range_approach,
                                      range_depart)  # stick them together
            number_range_samples = len(
                range_desired
            )  # total length is the number of samples we expect.

            #print(spectrogram.shape)

            spect_dis_approach = approach_inter(approach_times)
            spect_dis_depart = depart_inter(depart_times)

            approach_bins = np.digitize(spect_dis_approach, range_approach)

            depart_bins = np.digitize(spect_dis_depart, range_depart)

            approach_spect = range_spect(approach_bins, spectrogram)
            depart_spect = range_spect(depart_bins, spectrogram)
            #print(approach_spect.shape)
            #print(depart_spect.shape)
            #print(spectrogram)
            #print(times)
            #print(times.shape)
            range_spectrogram = np.concatenate((approach_spect, depart_spect),
                                               axis=1)
            ship.spect = range_spectrogram
            normal = True
            #ranges = get_ranges(approach_bins,depart_bins,range_approach,range_depart)
            #print(range_spectrogram)
            #print(ranges)
            #plt.yscale('log') #make y scale log to match the new decibel units
            #axes = plt.gca() #get axes object
            #axes.set_ylim([10,1000]) #set upper limit of data on axes to be 1000
            # plt.pcolormesh(ranges,frequencies,range_spectrogram,vmin=60,vmax=110 ) #plot the data and add color
            # plt.set_cmap('jet')
            # plt.ylabel('Frequency [Hz]')
            # plt.xlabel('Distance [km]')
            # locs, ticks = plt.xticks() #get current time ticks
            # new_ticks = get_ticks(ranges,locs)
            # plt.xticks(locs,new_ticks)

            # plt.colorbar()
            #plt.xticks(locs, new_ticks)  # Set locations and labels to the distance
            #plt.savefig(destination) #save spectrogram at destination
            #plt.imshow(spectrogram)
            #plt.show() #show plot
            #plt.close()
        except:
            print("bad ship")
            up.one_jar(folder, ship, True)
            normal = False
            pass
    # print("bad ships:")
    # print(bad_apples)
    # print(len(bad_apples))
        if normal:
            up.one_jar(folder, ship, False)
예제 #4
0
def generate(folder):
    ships = up.unpickle_ships(folder)
    bad_apples = []
    i = 0
    for ship in ships:
        try:
        
            wavfilepath = ship.filepath + ship.id + '.wav' #the original wav file
            txtfilepath = ship.filepath + ship.id + '.txt' #the original txt file
            destination =  destination_folder + ship.year_month +'\\' + ship.id + '.png' #the destination for the spectrogram
            print(txtfilepath)
            
            converted_times,start,cpa_index,cpa_time,normal = convert_time(ship) #convert all times and find the file start time and cpa time
            print(normal)
            after_start = ship.distance[start:] #find all distances after file_time 
            closest_range = np.min(np.abs(after_start)) # find closest point of approach (cpa)
            cpa_index = after_start.index(closest_range)
            if len(after_start) < 20:
                normal = False
                raise NameError('Too Few Distances')
            if normal: #for now only do the ones that are normal
                print('here 1')
                pre_cpa = after_start[:cpa_index]
                pre_times = converted_times[:cpa_index]
                approach_inter = interpolate.interp1d(pre_times,pre_cpa, axis=0, fill_value="extrapolate")
            
            post_cpa = after_start[cpa_index:] #find all distances after cpa time
            post_times = converted_times[cpa_index:]
            depart_inter = interpolate.interp1d(post_times,post_cpa, axis=0, fill_value="extrapolate")
            print('here 2')
            
            sample_rate, samples = wavfile.read(wavfilepath) #get original wav file samples at the original sample rate
            sound_length = len(samples)//sample_rate
           
            frequencies, times, spectrogram = signal.spectrogram(samples,sample_rate, window = np.hanning(10e3), noverlap = 0, nfft = 10e3, 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
            spectrogram = spectrogram + np.transpose(uppc) #add tf results to spect results

            range_step = .01 # step size of 1m
            if normal:
                approach_times = np.arange(0,cpa_time)
                range_approach = ((np.arange(pre_cpa[0], closest_range, -range_step))) # make a vector of distances between first range and cpa 
                depart_times = np.arange(cpa_time,sound_length)
            else:
                depart_times = np.arange(,sound_length)
            range_depart  = (np.arange(closest_range, post_cpa[len(post_cpa)-1], range_step)) # make a vector of distances between cpa and last range
            #range_desired = np.append(range_approach,range_depart)# stick them together
            #number_range_samples = len(range_desired)# total length is the number of samples we expect. 

            if normal:
                print('here 3')
                spect_dis_approach = approach_inter(approach_times)
                approach_bins = np.digitize(spect_dis_approach,range_approach)
                approach_spect = range_spect(approach_bins,spectrogram)
                
            print('here 4')
            spect_dis_depart = depart_inter(depart_times)
            depart_bins = np.digitize(spect_dis_depart,range_depart)
            
            #depart_bins = depart_bins + last_bin
            depart_spect = range_spect(depart_bins,spectrogram)
            if normal:
                print('here 5')
                range_spectrogram = np.concatenate((approach_spect,depart_spect),axis=1)
            else:
                print('here 6')
                range_spectrogram = depart_spect
            ship.spect = range_spectrogram
            normal = True
            #plt.savefig(destination) #save spectrogram at destination
         
            #plt.close()
        except:
                i+=1
                print("bad ship" + str(i))
                up.one_jar(folder,ship,True)
                normal = False
                pass
        if normal:
                up.one_jar(folder,ship,False)