コード例 #1
0
ファイル: cpu_detection.py プロジェクト: davidmam/batdetect
    def __init__(self, weight_file, params_file):
        """Performs detection on an audio file.
        The structure of the network is hard coded to a network with 2
        convolution layers with pooling, 1 or 2 fully connected layers, and a
        final softmax layer.

        weight_file is the path to the numpy weights to the network
        params_file is the path to the network parameters
        """

        self.weights = np.load(weight_file, encoding='bytes', allow_pickle=True)
        if not all([weight.dtype == np.float32 for weight in self.weights]):
            for i in range(self.weights.shape[0]):
                self.weights[i] = self.weights[i].astype(np.float32)

        with open(params_file, 'rb') as fp:
            params = pickle.load(fp)

        self.chunk_size = 4.0  # seconds
        self.win_size = params['win_size']
        self.max_freq = params['max_freq']
        self.min_freq = params['min_freq']
        self.slice_scale = params['slice_scale']
        self.overlap = params['overlap']
        self.crop_spec = params['crop_spec']
        self.denoise = params['denoise']
        self.smooth_spec = params['smooth_spec']
        self.nms_win_size = int(params['nms_win_size'])
        self.smooth_op_prediction_sigma = params['smooth_op_prediction_sigma']
        self.sp = Spectrogram()
コード例 #2
0
def loadFile(path, sr=16000, time=3.0):
    spectrogram = Spectrogram(input_shape=(int(time * sr), 1), sr=sr)
    X = []
    curr = 0  # indicates 1st and last file of speaker
    speaker_dict = {}

    print('Total speakers:', len(os.listdir(path)))
    c = 0

    for speaker in os.listdir(path):
        if c % 10 == 0:
            print('Speakers done:', c)
        c += 1
        print('Loading speaker: ' + speaker)
        speaker_path = os.path.join(path, speaker)
        speaker_dict[speaker] = [curr, None]
        X_speaker = []

        for file_path in os.listdir(speaker_path):
            files = os.path.join(speaker_path, file_path)
            audio, rate = librosa.load(files, sr=sr)
            audio = np.expand_dims(audio, axis=1)

            spec = spectrogram.spectrogram(audio)
            X_speaker.append(spec)
            curr += 1

        speaker_dict[speaker][1] = curr - 1
        X.append(X_speaker)

    return X, speaker_dict
コード例 #3
0
def saveBaselineIntensityImages(files,
                                saveLoc: str = None,
                                imageExt: str = "png"):
    """
    Save a image file of the baseline as a function of time to the folder specified by saveLoc.
    """
    if saveLoc == None:
        saveLoc = r"../baselineIntensityMaps"

    for i in tqdm.trange(len(files)):
        filename = f"../dig/{files[i]}"
        MySpect = Spectrogram(filename)
        peaks, _, heights = baselines_by_squash(MySpect)

        plt.plot(
            np.array(MySpect.time) * 1e6,
            MySpect.intensity[MySpect._velocity_to_index(peaks[0])])
        plt.xlabel("Time ($\mu$s)")
        plt.ylabel("Intensity (db)")
        plt.title(f"{MySpect.data.filename}")
        path = os.path.join(
            saveLoc,
            files[i].replace(".dig", "") + f"_baselineIntensity.{imageExt}")
        if not os.path.exists(os.path.split(path)[0]):
            os.makedirs(os.path.split(path)[0])

        plt.savefig(path)

        # Reset the state for the next data file.
        plt.clf()
        del MySpect
コード例 #4
0
def make_spectrogram(pipe: PNSPipe, **kwargs):
    """
    Compute a spectrogram using the passed kwargs or
    built-in defaults and set the pipe.spectrogram field.
    Default values are:
      wavelength = 1.55e-6
      points_per_spectrum = 4096
      overlap = 7/8
      window_function = None
      form = 'power'
    """
    from spectrogram import Spectrogram

    defaults = dict(
        t_start=kwargs.get('t_start'),
        ending=kwargs.get('ending'),
        wavelength=kwargs.get('wavelength', 1.55e-6),
        points_per_spectrum=kwargs.get('points_per_spectrum', 4096),
        overlap=kwargs.get('overlap', 7 / 8),
        window_function=kwargs.get('window_function'),
        form=kwargs.get('form', 'power'),
    )
    allargs = dict({**defaults, **kwargs})
    # list the arguments we use in the log
    for k, v in allargs.items():
        pipe.log(f"+ {k} = {v}")

    pipe.spectrogram = Spectrogram(pipe.df, **allargs)
コード例 #5
0
def createGallery(digsToLookAt: list = None,
                  colormap="blue-orange-div",
                  fileext="jpeg",
                  transformData=False):
    if type(digsToLookAt) == type(None):
        digsToLookAt = DigFile.inventory(justSegments=True)['file']

    digDir = DigFile.dig_dir()
    imageDir, _ = os.path.split(digDir)
    imageFold = "ImageGallery"
    imageDir = os.path.join(imageDir, imageFold)
    print(digsToLookAt)
    for i in range(len(digsToLookAt)):
        filename = digsToLookAt[i]
        print(filename)
        spec = Spectrogram(os.path.join(digDir, filename))
        pcms, lastAxes = spec.plot(transformData, cmap=colormap)
        fileloc, filename = os.path.split(
            os.path.splitext(os.path.join(imageDir, filename))[0])
        fileloc = os.path.join(fileloc, filename)

        for key in pcms.keys():
            if "complex" in key:
                continue  # There is not a graph with a name that contains complex.
            if not os.path.exists(fileloc):
                os.makedirs(fileloc)

            plt.figure(num=key)  # Get to the appropriate figure.
            plt.savefig(
                os.path.join(fileloc, filename) +
                f' {key} spectrogram.{fileext}')
            plt.clf()
        del spec
コード例 #6
0
    def __init__(self, file_pth: str, train=True, random_state=None):
        super(Dataset, self).__init__()

        self.sp = Spectrogram()

        self.file_pth = file_pth
        self.train = train
        self.random_state = None

        self.train = self.read_data()
コード例 #7
0
    def get_data(self):

        print('Reading Data...')

        file_list = os.listdir(self.params['data_path'])
        file_list = [
            os.path.join(self.params['data_path'], i) for i in file_list[:10]
        ]

        x_data = Spectrogram(filenames=file_list)

        self.x_train, self.x_test = train_test_split(x_data.spectrogram,
                                                     test_size=0.3)
コード例 #8
0
    def test(self, filenames):
        filenames = filenames[5:15]
        for filename in filenames:
            print(filename)
            test_data = Spectrogram(filenames=[filename])
            decoded_spectrogram = self.network.model.predict(
                test_data.spectrogram)
            #print_summary(self.network.model, line_length=80)
            test_data.spectrogram_to_wav(
                spectrogram=copy.deepcopy(decoded_spectrogram),
                filename=filename.split("/")[-1])

            test_data.visualize(filename=filename,
                                spectrogram=decoded_spectrogram)
コード例 #9
0
    def __init__(self, file_pth: str, train=True, random_state=None):
        super(Dataset, self).__init__()

        self.sp = Spectrogram()

        self.file_pth = file_pth
        self.train = train
        self.random_state = None

        self.data = self.read_data()
        datasets = train_test_split(self.data)
        if train:
            self.train = datasets[0]
        else:
            # actually this is test dataset
            self.train = datasets[1]
コード例 #10
0
def generate_graphs(directory):
    for file in os.listdir(directory):
        print(directory + file)
        if file.endswith(".dig"):
            # Then we have the right file.

            Spectrogram_Object = Spectrogram(directory + file)
            sampleSize = 256
            full_length_spectra = Spectrogram_Object.spectrogram(
                0, Spectrogram_Object.samples, sampleSize)

            colormaps = ["gist_stern", "tab20c", "tab20b", "terrain"]

            for colormap in colormaps:
                Spectrogram_Object.plot(full_length_spectra,
                                        sample_window=sampleSize,
                                        cmap=colormap)
コード例 #11
0
def setupForDocumentation():
    # Set up
    filename = "../dig/CH_4_009.dig"
    t1 = 14.2389/1e6
    t2 = 31.796/1e6
    MySpect = Spectrogram(filename, mode = "complex")
    sTime = MySpect._time_to_index(t1)
    eTime = MySpect._time_to_index(t2)

    bottomVel = 1906.38
    botInd = MySpect._velocity_to_index(bottomVel)

    topVel = 5000
    topVelInd = MySpect._velocity_to_index(topVel)

    visualSignalStart = 2652.21
    visSigIndex = MySpect._velocity_to_index(visualSignalStart)

    return MySpect, sTime, eTime, botInd, topVelInd, visSigIndex
コード例 #12
0
def setupForDocumentation():
    # Set up
    filename = "../See_if_this_is_enough_to_get_started/CH_4_009.dig"
    t1 = 14.2389/1e6
    t2 = 31.796/1e6
    MySpect = Spectrogram(filename)
    sTime = MySpect._time_to_index(t1)
    eTime = MySpect._time_to_index(t2)

    bottomVel = 1906.38
    botInd = MySpect._velocity_to_index(bottomVel)

    topVel = 5000
    topVelInd = MySpect._velocity_to_index(topVel)

    signalJump = 25

    visualSignalStart = 2652.21
    visSigIndex = MySpect._velocity_to_index(visualSignalStart)

    return MySpect, sTime, eTime, signalJump, botInd, topVelInd, visSigIndex
コード例 #13
0
def runAgainstTestSet(testSetFilename,
                      guessAtOptimalThres,
                      guessAtOptimalSkipUntil,
                      errorPower: int = 1):

    data = pd.read_excel(testSetFilename)

    # Ignore the samples that we do not have the full data for.
    data = data.dropna()
    data.reset_index()  # Reset so that it can be easily indexed.

    files = data["Filename"].to_list()

    bestGuessTimes = (data[data.columns[1]] * 1e6).to_list()
    # This will be the same times for the probe destruction dataset. It is generally ignored currently.
    bestGuessVels = data[data.columns[-1]].to_list()

    d = {"Filename": files}

    d["ProbeDestructionEstimateTime"] = np.zeros(len(files))
    d["Error Versus Label"] = np.zeros(len(files))

    for i in tqdm.trange(len(files)):
        filename = os.path.join(os.path.join("..", "dig"), f"{files[i]}")
        MySpect = Spectrogram(filename, overlap=0)
        peaks, _, heights = baselines_by_squash(MySpect, min_percent=0.75)
        timeEstimate = baselineTracking(MySpect, peaks[0], guessAtOptimalThres,
                                        guessAtOptimalSkipUntil)
        d["ProbeDestructionEstimateTime"][i] = timeEstimate
        d["Error Versus Label"][i] = np.power(bestGuessTimes[i] - timeEstimate,
                                              errorPower)

    errors = d["Error Versus Label"]

    print(f"The statistics for the test set specified by {testSetFilename}\n")
    print(
        f"Avg: {np.mean(errors)} Std: {np.std(errors)} Median: {np.median(errors)} Max: {np.max(errors)} Min: {np.min(errors)}"
    )

    return d
コード例 #14
0
    def __init__(self, user_dir, contains_vocals, n_samples):
        os.chdir(user_dir)
        # The line below makes a list of all the filenames in the specified directory,
        # while excluding names of subdirectories/folders
        self.filename_list = [name for name in os.listdir('.') if os.path.isfile(name)]
        print('Processing songs from:' + user_dir)
        # For test set, n_samples = 24505
        # For training set, n_samples = 46200
        freq_size = 513
        time_size = 23
        self.n_samples = n_samples
        # These are dimensions of the matrix [X, Y]

        self.X = np.zeros((self.n_samples, freq_size, time_size,1), dtype=int)
        self.Y = np.zeros((self.n_samples,1), dtype=int)
        # This will ensure we are indexing correctly to put the x for our sample into our X matrix of all songs' data
        n_samples_so_far = 0
        for i in range(len(self.filename_list)):
            filename = self.filename_list[i]  # Iterates through each file name in the list
            print(filename)

            self.contains_vocals = contains_vocals
            spect = Spectrogram(filename, contains_vocals)
            x_is = spect.get_X()
            y_is = spect.get_Y()
            #print("x_is"+str(x_is.shape))
            #print("y_is"+str(y_is.shape))
            #print("samples so far "+str(n_samples_so_far))
            
            n_song_samples = np.shape(y_is)[0] # I.e., how many samples did we make from the song
                                               # (song length (sec)*2, since 500ms segments used)

            upper_index = n_samples_so_far + n_song_samples  # Each sample is a row in our array/matrix
            #print("upper index "+str(upper_index))
            self.X[n_samples_so_far:upper_index, :, :,:] = x_is
            self.Y[n_samples_so_far:upper_index,:] = y_is

            n_samples_so_far = n_samples_so_far + n_song_samples  # Updates how many samples we've done so far
コード例 #15
0
    # sort the peaks and heights into descending order
    peaks, heights = peaks[ordering], heights[ordering]

    neighborhoods = []
    width = 50
    for peak_center in peaks:
        low, high = max(0, peak_center -
                        width), min(len(powers) - 1, peak_center + width)
        neighborhoods.append([velocities[low:high], powers[low:high]])
    return neighborhoods


if __name__ == '__main__':
    import os
    os.chdir('../dig')
    sgram = Spectrogram('./CH_4_009/seg00.dig', 0.0, 50.0e-6)


    peaks, uncertainties, peak_heights = baselines_by_squash(sgram)
    velos = []
    times = [x for x in range(0,30)]


    pcms, axes = sgram.plot(min_time=0, min_vel=100, max_vel=10000, cmap='3w_gby')


    # add the peaks to the plot and change the color map range
    pcm = pcms['intensity raw']
    pcm.set_clim(-40, -65)
    for peak in peaks:
        velo_index = sgram._velocity_to_index(peak)
コード例 #16
0
    WHITE_CH4_SHOT/seg00.dig -- ??? opencv_long_start_pattern4 span=200

    BLUE_CH1_SHOT/seg00.dig -- opencv_long_start_pattern4 span=150
    BLUE_CH2_SHOT/seg00.dig -- ??? opencv_long_start_pattern3 span=200
    BLUE_CH3_SHOT/seg00.dig -- opencv_long_start_pattern4 span=200

    CH_1_009/seg00.dig -- opencv_long_start_pattern2 span=200
    CH_3_009/seg00.dig -- opencv_long_start_pattern2 span=200
    CH_4_009/seg01.dig -- opencv_long_start_pattern2 span=200
    CH_4_009/seg02.dig -- opencv_long_start_pattern4 span=200
    """
    from ProcessingAlgorithms.preprocess.digfile import DigFile

    path = "../dig/BLUE_CH1_SHOT/seg00.dig"
    df = DigFile(path)
    spec = Spectrogram(df, 0.0, 60.0e-6, overlap_shift_factor= 1/8, form='db')
    spec.availableData = ['intensity']

    # print(spec.time[200])

    # gives user the option to click, by default it searches from (0,0)
    template_matcher = TemplateMatcher(spec,template=Templates.opencv_long_start_pattern5.value,
                                            span=200,
                                            k=20,
                                            methods=['cv2.TM_CCOEFF_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'])


    # masks the baselines to try and avoid matching with baselines or echoed signals
    template_matcher.mask_baselines()

    # get the times and velocities from matching
コード例 #17
0
        if x < 0:
            return 0
        if x > nmax:
            return nmax
        return x

    for t in range(len(time) - 1):
        if power[amax[t], t] > threshold:
            acenter = amax[t]
            vfrom, vto = peg(acenter - span), peg(acenter + span)
            gus = Gaussian(velocity[vfrom:vto], power[vfrom:vto, t])
            if gus.valid:
                times.append(time[t])
                centers.append(gus.center)
                widths.append(gus.width)
                amps.append(gus.amplitude)
            else:
                print(f"[{t}] {gus.error}")

    #power[power>=threshold] = 1
    plt.pcolormesh(time * 1e6, velocity, power)
    plt.plot(np.array(times) * 1e6, centers, 'r.', alpha=0.5)
    plt.show()


if __name__ == "__main__":
    sp = Spectrogram('../dig/sample')
    roi = dict(time=(13.6636e-6, 35.1402e-6), velocity=(2393.33, 4500.377))
    analyze_region(sp, roi)
    print("Hi!")
コード例 #18
0
            add_row(next(loc), 0.9, 0.8, 0.5)  # to faded yellow
            add_row(next(loc), 0.5, 0, 0.5)
            add_row(next(loc), 0, 0.5, 0)
            add_row(next(loc), 1, 0.75, 0.2)
            add_row(next(loc), 0.2, 0.2, 0.2)
            add_row(next(loc), 0, 0, 0)
        except:
            pass

        add_row(1, 0, 0, 0)
        return cdict

    roids = calculate_centroids()
    bounds = normalize_bounds(roids)
    cdict = build_cdict(bounds)

    from matplotlib.colors import LinearSegmentedColormap
    COLORMAPS[name] = LinearSegmentedColormap(name, cdict)
    return dict(name=name, centroids=roids, cmap=COLORMAPS[name])


if __name__ == '__main__':
    sg = Spectrogram('../dig/CH_4_009.dig')
    roids = make_spectrogram_color_map(sg, 4, "Kitty")

    sg.plot(cmap = roids["cmap"])
    import matplotlib.pyplot as plt
    plt.xlim((0, 50))
    plt.ylim((1500, 5000))
    plt.show()
コード例 #19
0
def runExperiment(trainingFilePath,
                  thresholds: list,
                  skipUntilTimes: list = [],
                  cmap: str = DEFMAP,
                  errorPower: int = 1):

    data = pd.read_excel(trainingFilePath)

    # Ignore the samples that we do not have the full data for.
    data = data.dropna()
    data.reset_index()  # Reset so that it can be easily indexed.

    files = data["Filename"].to_list()
    bestGuessTimes = (data[data.columns[1]] * 1e6).to_list()
    # This will be the same times for the probe destruction dataset. It is generally ignored currently.
    bestGuessVels = data[data.columns[-1]].to_list()

    d = {"Filename": files}

    if skipUntilTimes == []:
        skipUntilTimes = [12e-6]

    cmapAttempt = cmap
    if cmapAttempt in COLORMAPS.keys():
        cmapAttempt = COLORMAPS[cmapAttempt]

    for i in range(len(thresholds)):
        d[f"threshold {thresholds[i]}"] = np.zeros(
            (len(skipUntilTimes), len(files)))
        d[f"threshold {thresholds[i]} error"] = np.zeros(
            (len(skipUntilTimes), len(files)))

    print("Running the experiment")

    for i in tqdm.trange(len(files)):
        filename = os.path.join(os.path.join("..", "dig"), f"{files[i]}")
        MySpect = Spectrogram(filename, overlap=0)
        peaks, _, heights = baselines_by_squash(MySpect, min_percent=0.75)
        baselineInd = MySpect._velocity_to_index(peaks[0])
        intensity = MySpect.intensity[baselineInd]

        for skipTimeInd in range(len(skipUntilTimes)):
            skipXInds = MySpect._time_to_index(skipUntilTimes[skipTimeInd])

            outputTimeInds = np.array(baselineExperiment(
                intensity, thresholds, skipXInds),
                                      dtype=int)

            for thresInd, thres in enumerate(thresholds):
                timeEstimate = MySpect.time[outputTimeInds[thresInd]] * 1e6

                d[f"threshold {thres}"][skipTimeInd][i] = timeEstimate
                d[f"threshold {thres} error"][skipTimeInd][
                    i] = bestGuessTimes[i] - timeEstimate

        del MySpect

    # Compute summary statistics
    summaryStatistics = np.zeros((len(thresholds), len(skipUntilTimes), 5))
    stats = ["Avg", "Std", "Median", "Max", "Min"]

    print(f"Computing the summary statistics: [{stats}]")

    for i in tqdm.trange(len(thresholds)):
        for j in range(len(skipUntilTimes)):
            q = np.power(d[f"threshold {thresholds[i]} error"][j], errorPower)
            summaryStatistics[i][j][0] = np.mean(q)
            summaryStatistics[i][j][1] = np.std(q)
            summaryStatistics[i][j][2] = np.median(q)
            summaryStatistics[i][j][3] = np.max(q)
            summaryStatistics[i][j][4] = np.min(q)

    for i in tqdm.trange(len(stats)):
        fig = plt.figure()
        ax = plt.gca()

        pcm = ax.pcolormesh(np.array(skipUntilTimes) * 1e6,
                            thresholds,
                            summaryStatistics[:, :, i],
                            cmap=cmapAttempt)
        plt.title(f"{stats[i]} Error over the Files Tested")
        plt.ylabel("Thresholds")
        plt.xlabel("Time that you skip at the beginning ($\mu$s)")
        plt.gcf().colorbar(pcm, ax=ax)

        plt.show()  # Need this for Macs.

    summaryFolder = r"../JumpOffTimeEstimates"
    if not os.path.exists(summaryFolder):
        os.makedirs(summaryFolder)

    for i in range(len(thresholds)):
        q = pd.DataFrame(summaryStatistics[i])
        internal_a = str(thresholds[i]).replace(".", "_")
        saveSummaryFilename = f"summaryThresh{internal_a}.csv"
        q.to_csv(os.path.join(summaryFolder, saveSummaryFilename))

    return summaryStatistics, d
コード例 #20
0
    def __init__(self, *args, **kwargs):
        """
        If one passes in a single unnamed arg, it can either be a digfile,
        a string pointing to a digfile, or a two-dimensional ndarray.
        If we are founded on a dig file, it is possible to recompute
        things. Only a subset of operations are possible when we're
        based on a two-dimensional array, but perhaps that is sometimes
        desirable.
        """
        self.digfile = None
        if len(args) == 1:
            arg = args[0]
            if isinstance(arg, str):
                self.digfile = DigFile(arg)
            elif isinstance(arg, DigFile):
                self.digfile = arg
        if self.digfile == None and len(args):
            # Let's see if we have enough information to display a spectrogram
            # That means we have a two-dimensional ndarray, and possibly corresponding
            # time and velocity arrays. The signature would be (intensity, [times, velocities]).
            arg = args[0]
            if isinstance(arg, np.ndarray) and len(arg.shape) == 2:
                self._static = {
                    'intensity': arg,
                }

                if len(args) == 3:
                    self._static['time'] = np.asarray(args[1])
                    self._static['velocity'] = np.asarray(args[2])
                else:
                    self._static['time'] = np.arange(0, -1 + arg.shape[1])
                    self._static['velocity'] = np.arange(0, -1 + arg.shape[0])

            assert hasattr(
                self, '_static'
            ), "Inappropriate arguments passed to the SpectrogramWidget constructor"

        # If LaTeX is enabled in matplotlib, underscores in the title
        # cause problems in displaying the histogram. However, we can
        # solve this by not using latex in displaying the title, so there
        # is no need to alter characters here.
        self.title = "" if self.static else self.digfile.filename.split(
            '/')[-1]
        self.baselines = []

        # Compute the base spectrogram (do we really need this?)
        self.spectrogram = None
        if self.dig:
            self.spectrogram = Spectrogram(self.digfile, None, None, **kwargs)
            self.spectrogram_fresh = True  # flag for the first pass

            self.spectrogram.overlap = 0.875

        self.fig, axes = plt.subplots(nrows=1,
                                      ncols=2,
                                      sharey=True,
                                      squeeze=True,
                                      gridspec_kw=self._gspec)
        self.axSpectrogram, self.axSpectrum = axes

        self.subfig = None
        self.axTrack = None
        self.axSpare = None

        # At the moment, clicking on the image updates the spectrum
        # shown on the left axes. It would be nice to be more sophisticated and
        # allow for more sophisticated interactions, including the ability
        # to display more than one spectrum.
        self.fig.canvas.mpl_connect('button_press_event',
                                    lambda x: self.handle_click(x))
        self.fig.canvas.mpl_connect('key_press_event',
                                    lambda x: self.handle_key(x))

        self.spectrum(None, "")

        self.image = None  # we will set in update_spectrogram
        self.colorbar = None  # we will set this on updating, based on the

        self.peak_followers = []  # will hold any PeakFollowers
        self.spectra = []  # will hold spectra displayed at right
        self.spectra_in_db = True  # should spectra be displayed in db?

        self.controls = dict()  # widgets stored by name
        self.layout = None  # how the controls get laid out
        self.selecting = False  # we are not currently selecting a ROI
        self.roi = []  # and we have no regions of interest
        self.threshold = None
        self.make_controls(**kwargs)

        display(self.layout)
        self.update_spectrogram()
コード例 #21
0
        interesting, fixed = horizontal_lines(squashed)

        fig = plt.figure()
        ax1 = fig.add_subplot(1, 3, 1)
        im1 = ax1.pcolormesh(self.time * 1e6, self.velocity,
                             20 * np.log10(self.intensity))
        im1.set_cmap(COLORMAPS['3w_gby'])
        fig.colorbar(im1, ax=ax1)

        int2, fixed2 = horizontal_lines(20 * np.log10(self.intensity))

        ax2 = fig.add_subplot(1, 3, 2)
        im2 = ax2.pcolormesh(self.time * 1e6, self.velocity,
                             fixed2)
        im2.set_cmap(COLORMAPS['3w_gby'])
        fig.colorbar(im2, ax=ax2)
        ax3 = fig.add_subplot(1, 3, 3)
        im3 = ax3.pcolormesh(self.time * 1e6, self.velocity, fixed)
        im3.set_cmap(COLORMAPS['3w_gby'])
        fig.colorbar(im3, ax=ax3)
        plt.show()


if __name__ == '__main__':
    sp = Spectrogram('../dig/PDV_CHAN1BAK001')
    gf = GrossFeatures(sp, time_chunk=8)
    gf.k_means()



コード例 #22
0
                os.makedirs(fileloc)

            plt.figure(num=key)  # Get to the appropriate figure.
            plt.savefig(
                os.path.join(fileloc, filename) +
                f' {key} spectrogram.{fileext}')
            plt.clf()
        del spec


if __name__ == "__main__":
    # process command line args
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--file_name', type=str, default=False)
    args = parser.parse_args()

    #Just set directory here, I can't be bothered
    directory = '/home/lanl/Documents/dig/new/CH_1_009/'

    plt.rcParams["figure.figsize"] = [
        20, 10
    ]  # This is setting the default parameters to "20 by 10" inches by inches.
    # I would prefer if this was set to full screen.

    if args.file_name:
        spec = Spectrogram(directory + args.file_name)
        spec.plot()
        plt.savefig(args.file_name + '.png')
        plt.clf()
        del spec
コード例 #23
0
for ind in range(len(files)):
    d = {"Template": Templates}
    for k in range(len(keyedMethods)):
        d[f"{keyedMethods[k]} Time microseconds"] = zeros
        d[f"{keyedMethods[k]} Velocity (m/s)"] = zeros
        d[f"{keyedMethods[k]} L2 error"] = zeros
        d[f"{keyedMethods[k]} L1 error"] = zeros
        
    outputFiles[ind] = d
print("The experiment is now set up")
# Run the experiment.

for i in tqdm.trange(len(files)):
    filename = files[i]
    fname = os.path.join(baseFolder, filename)
    MySpect = Spectrogram(fname, overlap=7/8)

    bestGuessTime = bestGuessTimes[i]
    bestGuessVel = bestGuessVels[i]

    template_matcher = TemplateMatcher(MySpect, None, Templates.start_pattern.value, span = 200, k=1)

    valuesList = template_matcher.matchMultipleTemplates(methods, Templates)
    for ind in range(len(valuesList)):
        times, velos, scores, methodUsed = valuesList[ind]

        for k in range(len(times)):
            output[ind][keyedMethods[methodUsed[k]] + " Time microseconds"][i] = times[k]
            output[ind][keyedMethods[methodUsed[k]] + " Velocity (m/s)"][i] = velos[k]
            output[ind][keyedMethods[methodUsed[k]] + " L2 error"][i] = np.sqrt((times[k] - bestGuessTime)**2 + (velos[k] - bestGuessVel)**2)
            output[ind][keyedMethods[methodUsed[k]] + " L1 error"][i] = np.abs(times[k] - bestGuessTime) + np.abs(velos[k] - bestGuessVel)
コード例 #24
0
# importing os module
import os, os.path

DIR = "C:/Users/.../musdb18/test/Instrumental Versions"  #The directory where your songs are stored
os.chdir(
    DIR
)  #Sets working directory to where song files are; this is so we can load them and write the dataset file in there

#The line below makes a list of all the filenames in the specified directory, while excluding names of subdirectories/folders
filename_list = [name for name in os.listdir('.') if os.path.isfile(name)]

# In[4]:

print('Processing songs from:' + DIR)
os.chdir(DIR)

for i in range(1, len(filename_list)):
    filename = filename_list[i]
    #Iterates through each file name in the list
    print(filename)
    contains_vocals = 0
    #If the songs are instrumental/karaoke, it should be 0; if it has vocals, value should be 1
    Spectrogram(
        filename, 0
    )  #Calling the Spectrogram class. This is what creates/updates our dataset textfile

# In[5]:

filename_list
コード例 #25
0
ファイル: birdsong.py プロジェクト: maptube/gnome-birdsong
def buildXCSpectrogram(filename, fileid, speciesid, outDirSpectrogram,
                       outDirTrainingVectors):
    """
    Build spectrogram data for given file
    :param filename:
    :param fileid
    :param speciesid:
    :param outDirSpectrogram:
    :param outDirTrainingVectors:
    :return:
    """

    genSpec = Spectrogram()
    #pick up audio file, make the spectrogram from it and save spec, plus serialise the data for training
    genSpec.load(filename)
    #spectrogram computation on entire waveform file
    spectrogramDBFS = genSpec.spectrogramDBFS  #this is the db relative full scale power spectra
    spectrogramMag = genSpec.spectrogramMag  #and this is the raw linear power spectra
    freq = genSpec.freq  #these are the frequency bands that go with the above
    print("spec check 1: ", np.min(spectrogramDBFS[0]),
          np.max(spectrogramDBFS[0]))
    #now plot the data and save the training frames
    print("spectrogram feature frames: ", len(spectrogramDBFS))
    print("spectrogram=", np.shape(spectrogramDBFS))
    #At this point we have a number of possibilities. There is the spectrumDBFS, which is the DB relative
    #full scale log magnitude power spectrum, or the raw linear magnitude power spectrum which is just from
    #the raw fft data directly (magnitude of the im, re vector). Either of these can be median filtered
    #and then converted to a mel scale.
    genSpec.plotSpectrogram(
        spectrogramDBFS, freq,
        os.path.join(outDirSpectrogram, 'xc' + fileid + '_spec_dbfs.png'))
    genSpec.plotSpectrogram(
        spectrogramMag, freq,
        os.path.join(outDirSpectrogram, 'xc' + fileid + '_spec_mag.png'))
    spectrogramDBFS_Filt = genSpec.spectrogramMedianFilter(spectrogramDBFS)
    spectrogramMag_Filt = genSpec.spectrogramMedianFilter(spectrogramMag)
    genSpec.plotSpectrogram(
        spectrogramDBFS_Filt, freq,
        os.path.join(outDirSpectrogram, 'xc' + fileid + '_spec_dbfs_med.png'))
    genSpec.plotSpectrogram(
        spectrogramMag_Filt, freq,
        os.path.join(outDirSpectrogram, 'xc' + fileid + '_spec_mag_med.png'))
    #now you can do a mel frequency one off of either spectrogram[Mag|DBFS] or spectrogram[Mag|DBFS]_Filt (median filtered)
    #melfreq, spectrogramMels = genSpec.melSpectrum(spectrogramMag_Filt,freq)
    #print "mels=",np.shape(spectrogramMels)
    #genSpec.plotSpectrogram(spectrogramMels,melfreq,os.path.join(outDirSpectrogram,'xc'+fileid+'_spec_mel_mag_med.png'))
    #spectrogramMelsLog = genSpec.logSpectrogram(spectrogramMels) #this applies a log function to the magnitudes
    #genSpec.plotSpectrogram(spectrogramMelsLog,melfreq,os.path.join(outDirSpectrogram,'xc'+fileid+'_spec_mel_log_mag_med.png'))
    #and rms normalisation
    #and silence removal

    #serialise the data from the spectrogram here so we can go back to it quickly later if needed

    #Save vector data needed for learning
    output = open(
        os.path.join(outDirTrainingVectors,
                     speciesid + '_xc' + fileid + '_dbfs.pkl'), 'wb')
    pickle.dump(spectrogramDBFS, output)
    output.close()
    output = open(
        os.path.join(outDirTrainingVectors,
                     speciesid + '_xc' + fileid + '_mag.pkl'), 'wb')
    pickle.dump(spectrogramMag, output)
    output.close()
    output = open(
        os.path.join(outDirTrainingVectors,
                     speciesid + '_xc' + fileid + '_freq.pkl'), 'wb')
    pickle.dump(freq, output)
    output.close()

    print("spec check 2: ", np.min(spectrogramDBFS[0]),
          np.max(spectrogramDBFS[0]))

    return spectrogramDBFS, spectrogramMag, freq
コード例 #26
0
    allDigs = DigFile.inventory()[
        "file"]  # Just the files that are not segments.

    saveLoc = os.path.join(
        os.path.split(digFolder)[0], "TotalIntensityMaps\\Median\\")
    if not os.path.exists(saveLoc):
        os.makedirs(saveLoc)

    fractions = np.arange(10)
    fractionsL = len(fractions)

    data = np.zeros((fractionsL, len(allDigs)))
    data[:, 0] = fractions
    for i in range(len(allDigs)):
        filename = allDigs[i]
        path = os.path.join(digFolder, filename)
        spec = Spectrogram(path)
        inTen = np.sum(spec.intensity, axis=0)
        # Offset the values so that everything is non negative.
        inTen = inTen - np.min(inTen)

        for j in range(fractionsL):
            frac = np.power(10.0, -1 * fractions[j])
            data[j, i] = len(threshold(inTen, frac))

        del spec

    name = os.path.join(saveLoc, "NumberOfLowValuesVsFracOfMax.txt")

    np.savetxt(name, data)
コード例 #27
0
from spectrogram import Spectrogram

def get_file_name(instrument, pitch, index = "000", velocity = "050"):
	return "../nsynth-train/" + instrument + "/" + instrument + "_" \
			+ index + "-" + pitch + "-" + velocity + ".wav"

guitar = [get_file_name("guitar_acoustic", i) for i in \
				["055", "060", "080", "100"]]
keyboard = [get_file_name("keyboard_acoustic", i) for i in \
				["055", "060", "080", "100"]]

for f in guitar + keyboard:
	d = Spectrogram(f)
	d.Visualize("random")