Ejemplo n.º 1
0
    def trim(self,
             s,
             min_freq=0,
             max_freq=float('Inf'),
             min_time=0,
             max_time=float('Inf'),
             save_metadata=True):
        """Cuts some pieces of the spectrogram.

        Keeps only a desired rectangle in the frequency/time matrix
        associated with the spectrogram. By default, all arguments not
        provided don't cause any restriction on the trimmed region.

        Args:
            min_freq: minimum frequency to be kept. Default: 0.
            max_freq: maximum frequency to be kept. Default: inf.
            min_time: minimum time to be kept. Default: 0.
            max_time: maximum time to be kept. Default: inf.
            save_metadata: flag indicating whether the metadata should be
                           computed. Default: True.

        Returns:
            Trimmed Spectrogram object.
        """
        # Regard default parameters
        if max_freq > s.metadata.max_freq:
            max_freq = s.metadata.max_freq

        if max_time > s.metadata.max_time:
            max_time = s.metadata.max_time

        # Finds frequency and time bounds
        maxK = s.freq_bin(max_freq)
        minK = s.freq_bin(min_freq)
        maxT = s.time_bin(max_time)
        minT = s.time_bin(min_time)

        #print min_time, max_time, min_freq, max_freq

        new_s = spectrogram.Spectrogram()
        new_s.data = s.data[minK:maxK + 1, minT:maxT + 1]
        new_s.metadata.min_freq = s.freq_range(minK)[0]
        new_s.metadata.min_time = s.time_range(minT)[0]
        new_s.metadata.max_freq = s.freq_range(maxK)[0]
        new_s.metadata.max_time = s.time_range(maxT)[0]
        new_s.metadata.sampling_configuration = \
            s.metadata.sampling_configuration
        new_s.metadata.input_metadata = copy.deepcopy(s.metadata)

        new_s.metadata.method = md.Metadata(original_input=s.metadata.input,
                                            original_method=s.metadata.method,
                                            name='trim',
                                            min_freq=min_freq,
                                            max_freq=max_freq,
                                            min_time=min_time,
                                            max_time=max_time)
        if save_metadata:
            new_s.metadata.input = md.ObjectMetadata(s)

        return new_s
Ejemplo n.º 2
0
    def run(self, args):
        # Loads basis if present
        if args.basis is not None:
            b = ld.LinearDecomposition().load(args.basis)
        else:
            b = None

        if args.basis is not None and b.data.right != {}:
            print "Basis doesn't have empty right side. Ignoring it."

        # Size of the decomposition (used when finding a basis too)
        if args.size is None:
            args.size = [None, None, None] # Simulate 3 values
        for i in range(len(args.size)):
            if args.size[i] == 'None' or args.size[i] == 'null':
                args.size[i] = None

        # Gather input spectrograms
        s_list = []
        s_meta = []
        for filename in args.piece:
            with open(filename, 'rb') as handler:
                s_list.append(spectrogram.Spectrogram().load(handler))
                s_meta.append(md.FileMetadata(handler))

        # Converts arguments
        size = int(args.size[0]) if args.size[0] is not None else None
        instrument = args.size[1] if args.size[1] is not None else ''
        note = args.size[2] if args.size[2] is not None else ''

        # Decompose
        d = self.compute(s_list,
                         size,
                         instrument,
                         note,
                         b,
                         args.beta,
                         args.min_delta,
                         args.max_iterations,
                         False)

        # Associates an activation metadata with its given spectrogram's
        # metadata
        for k, data, metadata in d.right():
            metadata.spectrogram_input = s_meta[k[-1]]

        # Checks if basis was provided
        if b is not None:
            # If provided, adds it as basis metadata for each activation
            meta = md.FileMetadata(args.basis)
            for k, data, metadata in d.right():
                metadata.basis_input = meta
        else:
            # Otherwise, the basis was computed right now, so we set its
            # metadata with the list of all spectrograms' metadata
            d.metadata.left[(args.size[1], args.size[2])].spectrogram_input = \
                    s_meta

        d.save(args.outfile)
Ejemplo n.º 3
0
    def run(self, args):
        d = self.compute(spectrogram.Spectrogram().load(args.infile),
                         args.note, args.instrument, args.number_frames,
                         args.frame_skip, False)

        d.metadata.left[(args.instrument, args.note)].spectrogram_input = \
                md.FileMetadata(args.infile)

        d.save(args.outfile)
Ejemplo n.º 4
0
    def run(self, args):
        s = spectrogram.Spectrogram().load(args.infile)

        t = track.FeatureTrack()
        t.data = feats.centroid(s.data) * \
            s.metadata.sampling_configuration.ofs

        t.metadata.sampling_configuration = s.metadata.sampling_configuration
        t.metadata.feature = "Centroid"
        t.metadata.filename = s.metadata.input.name
        t.save(args.outfile)
Ejemplo n.º 5
0
    def run(self, args):
        s = spectrogram.Spectrogram().load(args.infile)

        t = self.calc_track(s)

        # t = track.FeatureTrack()
        # t.data = feats.flatness(s.data)
        #
        # t.metadata.sampling_configuration = s.metadata.sampling_configuration
        # t.metadata.feature = "Flatness"
        # t.metadata.filename = s.metadata.input.name

        t.save(args.outfile)
Ejemplo n.º 6
0
    def run(self, args):
        s = spectrogram.Spectrogram().load(args.infile)

        t = self.calc_track(s)

        # t = track.FeatureTrack()
        # t.data = feats.rolloff(s.data) * \
        #     s.metadata.sampling_configuration.ofs
        #
        # t.metadata.sampling_configuration = s.metadata.sampling_configuration
        # t.metadata.feature = "Roll-Off"
        # t.metadata.filename = s.metadata.input.name
        t.save(args.outfile)
Ejemplo n.º 7
0
    def run(self, args):

        s = spectrogram.Spectrogram().load(args.infile)

        t = self.calc_track(s, args.texture_length)

        #print s.data.shape

        # t = track.FeatureTrack()
        # t.data = feats.low_energy(s.data/s.metadata.sampling_configuration.dft_length, args.texture_length)
        #
        # t.metadata.sampling_configuration = s.metadata.sampling_configuration
        # t.metadata.feature = "LowEnergy"
        # t.metadata.filename = s.metadata.input.name
        t.save(args.outfile)
Ejemplo n.º 8
0
    def run(self, args):
        s = spectrogram.Spectrogram().load(args.infile)

        t = track.FeatureTrack()

        H = self.build_filterbank(args.central_frequencies, s)

        t.data = numpy.dot(H, s.data).T

        t.metadata.sampling_configuration = s.metadata.sampling_configuration
        t.metadata.feature = ""
        for f in args.central_frequencies:
            t.metadata.feature += "FB_" + str(f) + "Hz "

        t.metadata.filename = s.metadata.input.name
        t.save(args.outfile)
Ejemplo n.º 9
0
    def fold(self, s, folds=2, save_metadata=True):
        """Cuts some pieces of the spectrogram.

        Keeps only a desired rectangle in the frequency/time matrix
        associated with the spectrogram. By default, all arguments not
        provided don't cause any restriction on the trimmed region.

        Args:
            folds: number of folds (tensor depth)
            save_metadata: flag indicating whether the metadata should be
                           computed. Default: True.

        Returns:
            Trimmed Spectrogram object.
        """

        #print min_time, max_time, min_freq, max_freq
        new_s = spectrogram.Spectrogram()
        new_s.data = s.data[:, 0:-folds]

        nFolds = 1
        while nFolds < folds:
            new_s.data = numpy.vstack(
                (new_s.data, s.data[:, nFolds:-folds + nFolds]))
            nFolds += 1

        new_s.metadata.min_freq = s.metadata.min_freq
        new_s.metadata.min_time = s.metadata.min_time
        new_s.metadata.max_freq = s.metadata.max_freq * folds
        new_s.metadata.max_time = s.metadata.max_time
        new_s.metadata.sampling_configuration = \
            s.metadata.sampling_configuration
        new_s.metadata.method = md.Metadata(original_input=s.metadata.input,
                                            original_method=s.metadata.method,
                                            name='tensor-fold',
                                            min_freq=s.metadata.min_freq,
                                            max_freq=s.metadata.max_freq *
                                            folds,
                                            min_time=s.metadata.min_time,
                                            max_time=s.metadata.max_time)
        if save_metadata:
            s.metadata.input = md.ObjectMetadata(s)

        return new_s
Ejemplo n.º 10
0
def plot(input_filename, output_filename, size=(3.45, 2.0)):
    """Plots the a spectrogram to an output file
    """
    s = spectrogram.Spectrogram().load(input_filename)

    d = s.data
    d = d / numpy.max(d)
    d = 1 - d

    min_freq = s.metadata.min_freq
    max_freq = s.metadata.max_freq
    min_time = s.metadata.min_time
    max_time = s.metadata.max_time

    im = plt.imshow(d, aspect='auto', origin='lower', cmap=plt.cm.gray,\
            extent=[min_time, max_time, min_freq/1000.0, max_freq/1000.0])
    plt.xlabel('Time (s)')
    plt.ylabel('Frequency (kHz)')
    fig = plt.gcf()
    width_inches = size[0]  #/80.0
    height_inches = size[1]  #/80.0
    fig.set_size_inches((width_inches, height_inches))
    plt.savefig(output_filename, bbox_inches='tight')
Ejemplo n.º 11
0
    parser.add_argument('-f','--minimum_frequency', 
        default=0, help="""Minimum frequency (Hz) to plot""")
    parser.add_argument('-F','--maximum_frequency', 
        default=2000, help="""Maximum frequency (Hz) to plot""")
    parser.add_argument('-t','--minimum_time', 
        default=0, help="""Minimum time (s) to plot""")
    parser.add_argument('-T','--maximum_time', 
        default=None, help="""Maximum time (s) to plot""")
    parser.add_argument('-W', '--width',
        default=400, help="""Plot width (px)""")
    parser.add_argument('-H', '--height',
        default=200, help="""Plot height (px)""")

    args = parser.parse_args()
    
    s = spectrogram.Spectrogram()
    s = s.load(open(args.infile))

    maxK = s.freq_bin(args.maximum_frequency)
    minK = s.freq_bin(args.minimum_frequency)
    if args.maximum_time is not None:
        maxT = s.time_bin(args.maximum_time)

    else:
        maxT = s.data.shape[1]

    maxTs = maxT / s.metadata.sampling_configuration.ofs
    minT = s.time_bin(args.minimum_time)
    
    out = s.data[minK:maxK, minT:maxT]
    out = out/numpy.max(out)
Ejemplo n.º 12
0
 def run(self, args):
     new_s = self.fold(spectrogram.Spectrogram().load(args.infile),
                       args.folds, True)
     new_s.save(args.outfile)
Ejemplo n.º 13
0
    def convert(self,
                wav_file,
                window_length=2048,
                dft_length=2048,
                window_step=1024,
                spectrum_type='magnitude',
                save_metadata=True,
                wav_rate=None,
                wav_data=None):
        """Converts a WAV file to a spectrogram.

        Args:
            wav_file: handler for an open wav file.
            window_length: window length for dft, in samples. Default: 2048.
            dft_length: dft length used. Default: 2048.
            window_step: step between windows, in samples. Default: 1024.
            spectrum_type: type of spectrum extracted. Default: 'magnitude'.
            save_metadata: flag indicating whether the metadata should be
                           computed. Default: True.

        Returns:
            Spectrogram object.
        """
        # Sets metadata
        s = spectrogram.Spectrogram()
        s.metadata.sampling_configuration.window_length = window_length
        s.metadata.sampling_configuration.dft_length = dft_length
        s.metadata.sampling_configuration.window_step = window_step
        s.metadata.sampling_configuration.spectrum_type = spectrum_type

        if wav_data is None:
            from_data = False
        else:
            from_data = True

        if isinstance(wav_file, basestring):
            wav_file = open(wav_file, "rb")

        if save_metadata:
            s.metadata.input = md.FileMetadata(wav_file)

        # Calculates data
        if not from_data:
            rate, data = self.load_audio(wav_file, fs=wav_rate)
        else:
            rate = wav_rate
            data = wav_data

        #data /= numpy.max(numpy.abs(data)) # Normalization to -1/+1 range

        data -= numpy.mean(data)
        data /= numpy.var(data)**(0.5)

        if data.ndim > 1:
            data = numpy.sum(data, axis=1)

        s.metadata.min_time = 0.0
        s.metadata.min_freq = 0.0
        s.metadata.max_time = len(data) / float(rate)
        s.metadata.max_freq = 0.5 * rate

        s.metadata.sampling_configuration.fs = rate
        s.metadata.sampling_configuration.ofs = \
                s.metadata.sampling_configuration.fs / \
                float(s.metadata.sampling_configuration.window_step)

        nSamples = len(data)

        if nSamples == 0:
            raise ValueError("File '%s' has no audio" % wav_file.name)

        window = scipy.signal.hamming(dft_length, sym=False)

        #magnitude spectrum is the absolute value (real part) of the complex spectrum
        magnitude_spectrum = numpy.abs(
            self.stft(data,
                      n_fft=dft_length,
                      hop_length=window_step,
                      win_length=window_length,
                      window=window,
                      center=True))

        if s.metadata.sampling_configuration.spectrum_type == 'sqrt':
            magnitude_spectrum = numpy.sqrt(magnitude_spectrum)

        if s.metadata.sampling_configuration.spectrum_type == 'power':
            magnitude_spectrum = magnitude_spectrum**2

        if s.metadata.sampling_configuration.spectrum_type == 'log':
            magnitude_spectrum = numpy.log10(
                numpy.sqrt(magnitude_spectrum) + 10**(-6))

        if s.metadata.sampling_configuration.spectrum_type == 'db':
            magnitude_spectrum = 20 * numpy.log10(magnitude_spectrum +
                                                  numpy.finfo(numpy.float).eps)

        s.data = copy.deepcopy(magnitude_spectrum)
        # print type(s.data), s.data.shape
        #pylab.show()

        return s
Ejemplo n.º 14
0
    def run(self, args):
        s = spectrogram.Spectrogram().load(args.infile)
        t = self.calc_track(s)

        t.save(args.outfile)
Ejemplo n.º 15
0
 def run(self, args):
     s = spectrogram.Spectrogram().load(args.infile)
     for i in range(s.data.shape[0]):
         for j in range(s.data.shape[1]):
             print '%e' % s.data[i, j],
         print ''
Ejemplo n.º 16
0
 def run(self, args):
     new_s = self.trim(spectrogram.Spectrogram().load(args.infile),
                       args.minimum_frequency, args.maximum_frequency,
                       args.minimum_time, args.maximum_time, False)
     new_s.metadata.input = md.FileMetadata(args.infile)
     new_s.save(args.outfile)