예제 #1
0
    def analyze(cls, samples, sample_rate, nlpc=9):

        if (rms(samples) < 5): return 0

        """
        duration = len(samples) * 1.0 / sample_rate  * 1000
        if duration < 30:
            return 0
        """

        shorts = [int(x / 1.0 * 32768) for x in samples]
        #print "samples", len(shorts), sample_rate, max(shorts)

        shorts = shorts * np.hamming(len(shorts))
        shorts = lfilter([1], [1, .63], shorts) # pre-emphasize that stuff
        A = lpc.lpc_ref(shorts, nlpc)
        roots = np.roots(A)
        roots = roots[np.imag(roots)>=0]
        #print len(roots), "roots"
        bw = -.5 * sample_rate / 2 / math.pi * np.log(np.abs(roots))
        angs = np.arctan2(np.imag(roots), np.real(roots))
        angs = angs * [sample_rate / 2 / math.pi]
        #angs = np.absolute(angs)
        order = np.argsort(angs)

        formants = []
        for i in order:
            if angs[i] > 90 and bw[i] < 400:
                formants.append(angs[i])
               
        #print "debug", formants
        if len(formants) > 0:
            """
            if formants[0] > 250 and formants[0] < 600:
                freqz= scipy.signal.freqz(1, A, 512)
                #print np.abs(freqz)
                pickle.dump( freqz, open( "freqz.p", "wb" ) )
            """
            return formants[:2]
        return None
예제 #2
0
    def analyze(cls, samples, sample_rate, nlpc=9):

        if (rms(samples) < 5): return 0
        """
        duration = len(samples) * 1.0 / sample_rate  * 1000
        if duration < 30:
            return 0
        """

        shorts = [int(x / 1.0 * 32768) for x in samples]
        #print "samples", len(shorts), sample_rate, max(shorts)

        shorts = shorts * np.hamming(len(shorts))
        shorts = lfilter([1], [1, .63], shorts)  # pre-emphasize that stuff
        A = lpc.lpc_ref(shorts, nlpc)
        roots = np.roots(A)
        roots = roots[np.imag(roots) >= 0]
        #print len(roots), "roots"
        bw = -.5 * sample_rate / 2 / math.pi * np.log(np.abs(roots))
        angs = np.arctan2(np.imag(roots), np.real(roots))
        angs = angs * [sample_rate / 2 / math.pi]
        #angs = np.absolute(angs)
        order = np.argsort(angs)

        formants = []
        for i in order:
            if angs[i] > 90 and bw[i] < 400:
                formants.append(angs[i])

        #print "debug", formants
        if len(formants) > 0:
            """
            if formants[0] > 250 and formants[0] < 600:
                freqz= scipy.signal.freqz(1, A, 512)
                #print np.abs(freqz)
                pickle.dump( freqz, open( "freqz.p", "wb" ) )
            """
            return formants[:2]
        return None
예제 #3
0
def f(q, RATE):
    import matplotlib.pyplot as plt
    import numpy as np
    import lpc
    import math
    from scipy.signal import lfilter
    plt.ion()

    N = 100
    TOP = 0

    values = np.array([0.0] * N)
    print "looping"
    l = None
    shorts = None
    while True:
        qs = q.qsize()
        # if qs>0:
        #     print "q @ ", qs
        try:
            newshorts = q.get_nowait()
            shorts = newshorts
            #print "drain"
        except EQ.Empty:
            if (shorts != None):
                """
                ac= np.correlate(shorts, shorts, mode='full')
                ac = ac[len(ac)/2:]
                peaks = (np.diff(np.sign(np.diff(ac))) < 0).nonzero()[0] + 1
                peaks_hz = [RATE/x for x in peaks[:3]]
                pstr= ", ".join(["%s hz"%(RATE / x) for x in peaks[:3]])
                plt.title(pstr, fontsize=14, fontweight='bold')
                """

                shorts = shorts * np.hamming(len(shorts))
                shorts = lfilter([1], [1, .63], shorts)
                A = lpc.lpc_ref(shorts, 9)
                roots = np.roots(A)
                roots = roots[np.imag(roots)<0]
                bw= -.5 * RATE / 2 / math.pi * np.log(np.abs(roots))
                angs = np.arctan2(np.imag(roots), np.real(roots))
                angs = angs * [RATE / 2 / math.pi]
                angs = np.absolute(angs)
                order = np.argsort(angs)
                #for v in  zip(angs, bw): print v
                #print "\n\n***"
                tstr= "    ".join(["{0:10d} hz".format(int(angs[order[i]])) for i in range(len(order)) if bw[order[i]] < 400])


                plt.title(tstr, fontsize=14, fontweight='bold')

                #print "lpc", A



                fft = np.fft.rfft(shorts)
                afft = np.absolute(fft)
                #sizeprint np.arange(len(fft)), np.absolute(fft)
                plt.clf()
                plt.xlim([0,3000])
                plt.ylim([0, 3000])
                print len(angs)
                print [angs[order[i]] for i in range(len(order))]
                h=plt.plot(angs[order[1]], angs[order[2]], 'ro')
                if np.mean(shorts**2) > 10e-8:
                    plt.draw()
예제 #4
0
    def encode(self, frame):
        import lpc
        # Apply window
        frame *= self.frame_window

        # Generate LPC coefficients
        lpc_error_coeffs = lpc.lpc_ref(frame, self.lpc_order)

        out_fc_indexes, out_ac_indexes = [], []
        out_fc_amplifs, out_ac_amplifs = [], []

        # Buld the noise weigthing filter W matrix
        # W(z) = A(z/weigthing_coeff_1) / A(z/weigthing_coeff_2)
        weigthing_b = lpc_error_coeffs * np.power(
            self.weigthing_coeff_1, np.arange(self.lpc_order + 1))
        weigthing_a = lpc_error_coeffs * np.power(
            self.weigthing_coeff_2, np.arange(self.lpc_order + 1))
        w = lfilter(weigthing_b, weigthing_a, self.delta)
        W = scipy.linalg.toeplitz(
            w,
            np.concatenate(
                ([w[0]], self.zero_input[:self.subframe_length - 1])))

        # Buid the H matrix = 1 / A
        h = lfilter([1], lpc_error_coeffs, self.delta)
        H = scipy.linalg.toeplitz(
            h,
            np.concatenate(
                ([h[0]], self.zero_input[:self.subframe_length - 1])))

        for subframe in frame.reshape(
            (self.n_subframes, self.subframe_length)):
            lpc_filtered = lfilter([1], lpc_error_coeffs,
                                   np.concatenate(
                                       (self._excitation,
                                        np.zeros(self.subframe_length))))

            z0 = lpc_filtered[
                self.subframe_length:]  # Zero input response for the H filter
            z1 = lfilter(
                weigthing_b,
                weigthing_a,  # Zero input response for the W filter
                np.concatenate(
                    (lpc_filtered[:self.subframe_length],
                     np.zeros(self.subframe_length))))[self.subframe_length:]

            # Search the best adaptive signal
            M = np.dot(W, H)
            d = np.dot(W, subframe - z0) + z1
            ac_index, ac_amplif = search_codebook(M, d, self.adaptive_codebook)
            ac_excitation = self.adaptive_codebook[ac_index] * ac_amplif

            # Search the best fixed codebook signal
            d = np.dot(W, subframe - z0 - np.dot(H, ac_excitation) + z1)
            fc_index, fc_amplif = search_codebook(M, d, self.fixed_codebook)
            fc_excitation = fc_amplif * self.fixed_codebook[fc_index]

            # Build current excitation using adaptive and fixed codebooks
            self._excitation = ac_excitation + fc_excitation

            # Append current excitation to adaptive codebook
            self.adaptive_codebook.add_vector(self._excitation)

            # store output values for this frame
            out_fc_indexes.append(fc_index)
            out_fc_amplifs.append(fc_amplif)
            out_ac_indexes.append(ac_index)
            out_ac_amplifs.append(ac_amplif)

        # generate binary output string with all output values
        lpc = lpc_error_coeffs.astype(self.lpc_error_coefs_dtype).tostring()
        fc_indexes = np.array(out_fc_indexes,
                              dtype=self.index_dtype).tostring()
        fc_amplifs = np.array(out_fc_amplifs,
                              dtype=self.amplifs_dtype).tostring()
        ac_indexes = np.array(out_ac_indexes,
                              dtype=self.index_dtype).tostring()
        ac_amplifs = np.array(out_ac_amplifs,
                              dtype=self.amplifs_dtype).tostring()

        return lpc + fc_indexes + fc_amplifs + ac_indexes + ac_amplifs
예제 #5
0
def f(q, RATE):
    import matplotlib.pyplot as plt
    import numpy as np
    import lpc
    import math
    from scipy.signal import lfilter
    plt.ion()

    N = 100
    TOP = 0

    values = np.array([0.0] * N)
    print "looping"
    l = None
    shorts = None
    while True:
        qs = q.qsize()
        # if qs>0:
        #     print "q @ ", qs
        try:
            newshorts = q.get_nowait()
            shorts = newshorts
            #print "drain"
        except EQ.Empty:
            if (shorts != None):
                """
                ac= np.correlate(shorts, shorts, mode='full')
                ac = ac[len(ac)/2:]
                peaks = (np.diff(np.sign(np.diff(ac))) < 0).nonzero()[0] + 1
                peaks_hz = [RATE/x for x in peaks[:3]]
                pstr= ", ".join(["%s hz"%(RATE / x) for x in peaks[:3]])
                plt.title(pstr, fontsize=14, fontweight='bold')
                """

                shorts = shorts * np.hamming(len(shorts))
                shorts = lfilter([1], [1, .63], shorts)
                A = lpc.lpc_ref(shorts, 9)
                roots = np.roots(A)
                roots = roots[np.imag(roots) < 0]
                bw = -.5 * RATE / 2 / math.pi * np.log(np.abs(roots))
                angs = np.arctan2(np.imag(roots), np.real(roots))
                angs = angs * [RATE / 2 / math.pi]
                angs = np.absolute(angs)
                order = np.argsort(angs)
                #for v in  zip(angs, bw): print v
                #print "\n\n***"
                tstr = "    ".join([
                    "{0:10d} hz".format(int(angs[order[i]]))
                    for i in range(len(order)) if bw[order[i]] < 400
                ])

                plt.title(tstr, fontsize=14, fontweight='bold')

                #print "lpc", A

                fft = np.fft.rfft(shorts)
                afft = np.absolute(fft)
                #sizeprint np.arange(len(fft)), np.absolute(fft)
                plt.clf()
                plt.xlim([0, 3000])
                plt.ylim([0, 3000])
                print len(angs)
                print[angs[order[i]] for i in range(len(order))]
                h = plt.plot(angs[order[1]], angs[order[2]], 'ro')
                if np.mean(shorts**2) > 10e-8:
                    plt.draw()

                #rms = sum([x**2 for x in shorts])**.5 /  len(shorts)
                #news.append(rms)
    if len(news) > 0 and False:
        values = np.roll(values, -1 * len(news))
        values[-1 * len(news):] = news
        l.set_ydata(values)
        TOP = max(TOP, max(values))
        plt.ylim([0, TOP])
        plt.draw()