Exemple #1
0
    def __calcCutOffIndex(self):

        self.wavFile = Sndfile(self.wavFileName, 'r')
        output = amplitude.output_signal(self.intervalWidth, 0, self.wavFile)

        frequencies, amplitudes = fourier.getFFT(self.sampling_interval, output)

        spectrumWidth = -abs(self.spectrumWidth)

        cutOffIndex = -1

        for i in range(np.size(frequencies)-1):
            if ((frequencies[i]<spectrumWidth) and (frequencies[i+1]>spectrumWidth)
                    or frequencies[i]==spectrumWidth 
                    or (frequencies[i]>spectrumWidth) and (frequencies[i+1]<spectrumWidth)):
                cutOffIndex = i
                break

        if (cutOffIndex==-1):
            print("given frequency was not found")
            cutOffIndex = 0 # Don't cut the spectrum

        self.cutOffIndex = cutOffIndex
        del self.wavFile
Exemple #2
0
    def __calcFreqAmps(self, narrow=False, compress=False, intervalSize=10, compressionMethodString="maxMedianDifference"): 
        # Clear calculated data if already exists
        if (compress):
            self.compressedNarrowAmplitudes = []
            self.compressedNarrowFrequencies = []
        elif (narrow):
            self.narrowAmplitudes = []
            self.narrowFrequencies = []         
        else:
            self.amplitudes = []
            self.frequencies = []

        self.times = []

        # Compression is set to only work with narrowed data: compression of the entire spectrum is never interesting
        if compress:
            print("Compressing works only with narrowed data")
            narrow = True

        # Find the place where the cutOff should occur if the spectrum should be narrowed down
        if narrow:
            self.__calcCutOffIndex()
        
        # Read the wav file
        self.wavFile = Sndfile(self.wavFileName, 'r')

        # Get the intervals
        for intervalStartFrame in range(self.start, self.end, self.intervalStartFrequency):
            startTime = intervalStartFrame*self.sampling_interval
            endTime = (intervalStartFrame+self.intervalWidth)*self.sampling_interval
            self.times.append((startTime+endTime)/2.0)

            output = amplitude.output_signal(self.intervalWidth, intervalStartFrame, self.wavFile)
            frequencies, amplitudes = fourier.getFFT(self.sampling_interval, output)

            if (narrow):
                frequencies, amplitudes = self.__narrowSpectrum(frequencies, amplitudes, self.cutOffIndex)

            if (compress):
                frequencies, amplitudes = self.__compressSpectrum(frequencies, amplitudes, intervalSize, compressionMethodString)

            if (compress):
                self.compressedNarrowAmplitudes.append(amplitudes)
                self.compressedNarrowFrequencies = frequencies
            elif (narrow):
                self.narrowAmplitudes.append(amplitudes)
                self.narrowFrequencies = frequencies
            else:
                self.amplitudes.append(amplitudes)
                self.frequencies = frequencies

            print (intervalStartFrame/self.fs),"/",self.end/self.fs

        self.wavFile.close()
        del self.wavFile

        if (compress):
            self.compressedNarrowDataExists = True
        elif (narrow):
            self.narrowDataExists = True
            self.saveNarrowFrequencyAmplitudes()
        else:
            self.dataExists = True