Exemplo n.º 1
0
    def compareAll(self, data):

        numOfSamples = len(data)

        confusionMap = np.ones([numOfSamples, numOfSamples])
        #        matchMap = np.ones([numOfSamples, numOfSamples])
        matchMap = np.zeros([numOfSamples, numOfSamples])

        xMap = np.zeros([numOfSamples, numOfSamples])

        ACs = self.processAll(data)

        for i, di in enumerate(data):
            iAC = ACs[i]
            iValue = di[1][0]

            for j, dj in enumerate(data):
                if i > j - 1:
                    jValue = dj[1][0]
                    jAC = ACs[j]
                    globalDistance, expD, route, expdRoute = self.compareAC(
                        iAC, jAC)
                    confusionMap[i, j] = globalDistance
                    confusionMap[j, i] = globalDistance
                    matchMap[i,
                             j] = (jValue == iValue) and (globalDistance < 0.8)
                    xMap[i, j] = (globalDistance < 0.5)
                    print("   -> ", i, j, "   -  ", di[1][0], " v ", dj[1][0],
                          "  \t", round(globalDistance, 3))

        self.p.imShow(confusionMap, "confusionMap ")
        self.p.imShow(matchMap, "matchMap ")
        self.p.imShow(xMap, "xMap ")
Exemplo n.º 2
0
    def compareTestToTrain(self, test, train, visu=False):

        testACs = self.processAll(test)
        trainACs = self.processAll(train)

        rows = len(test)
        cols = len(train)
        scoreMap = np.zeros([rows, cols])
        matchMap = np.zeros([rows, cols])
        matchScoreMap = np.zeros([rows * 3, cols]) - np.inf

        #        print("compareTestToTrain 1 - ", testACs)

        matchCount = 0
        testCount = 0

        print("compareTestToTrain  - ", len(test[0]), len(train[0]))
        for i, iTest in enumerate(test):
            iexp = i * 3

            iTestAC = testACs[i]
            scores = []
            for j, jTrain in enumerate(train):
                jTrainAC = trainACs[j]
                globalDistance, expD, route, expdRoute = self.compareAC(
                    iTestAC, jTrainAC)

                scoreMap[i, j] = globalDistance
                matchScoreMap[iexp, j] = globalDistance

                scores.append(globalDistance)
                isSame = iTest[1][0] == jTrain[1][0]
                matchScoreMap[iexp + 1, j] = 1 if isSame else np.inf


#                print("   -> ", i, j, "   -  ", iTest[1][0]," v ", jTrain[1][0], "  \t",round(globalDistance, 3) )

            lowestScoreIdx = np.argmin(scores)

            isMatch = iTest[1][0] == train[lowestScoreIdx][1][0]

            matchCount = matchCount + (1 if isMatch else 0)
            testCount = testCount + 1

            print("   -> ", i, lowestScoreIdx, "   -  ", iTest[1][0], " v ",
                  train[lowestScoreIdx][1][0], "  \t",
                  round(scores[lowestScoreIdx], 3), "  \t", isMatch)

            matchMap[i, lowestScoreIdx] = 1 * (1 if isMatch else -1)
            matchScoreMap[iexp + 1,
                          lowestScoreIdx] = 1 * (2 if isMatch else -0.5)

        matchRatio = (matchCount / testCount) if testCount != 0 else 0

        self.p.imShow(scoreMap, "scoreMap ")

        self.p.imShow(matchMap, "matchMap ")
        self.p.imShow(matchScoreMap, "matchScoreMap ")
        print(" matchRatio ", matchRatio, "    ", matchCount, testCount)
Exemplo n.º 3
0
 def lpc_to_lsf(self, all_lpc):
     if len(all_lpc.shape) < 2:
         all_lpc = all_lpc[None]
     order = all_lpc.shape[1] - 1
     all_lsf = np.zeros((len(all_lpc), order))
     for i in range(len(all_lpc)):
         lpc = all_lpc[i]
         lpc1 = np.append(lpc, 0)
         lpc2 = lpc1[::-1]
         sum_filt = lpc1 + lpc2
         diff_filt = lpc1 - lpc2
 
         if order % 2 != 0:
             deconv_diff, _ = sg.deconvolve(diff_filt, [1, 0, -1])
             deconv_sum = sum_filt
         else:
             deconv_diff, _ = sg.deconvolve(diff_filt, [1, -1])
             deconv_sum, _ = sg.deconvolve(sum_filt, [1, 1])
 
         roots_diff = np.roots(deconv_diff)
         roots_sum = np.roots(deconv_sum)
         angle_diff = np.angle(roots_diff[::2])
         angle_sum = np.angle(roots_sum[::2])
         lsf = np.sort(np.hstack((angle_diff, angle_sum)))
         if len(lsf) != 0:
             all_lsf[i] = lsf
     return np.squeeze(all_lsf)
Exemplo n.º 4
0
 def lsf_to_lpc(self, all_lsf):
     if len(all_lsf.shape) < 2:
         all_lsf = all_lsf[None]
     order = all_lsf.shape[1]
     all_lpc = np.zeros((len(all_lsf), order + 1))
     for i in range(len(all_lsf)):
         lsf = all_lsf[i]
         zeros = np.exp(1j * lsf)
         sum_zeros = zeros[::2]
         diff_zeros = zeros[1::2]
         sum_zeros = np.hstack((sum_zeros, np.conj(sum_zeros)))
         diff_zeros = np.hstack((diff_zeros, np.conj(diff_zeros)))
         sum_filt = np.poly(sum_zeros)
         diff_filt = np.poly(diff_zeros)
 
         if order % 2 != 0:
             deconv_diff = sg.convolve(diff_filt, [1, 0, -1])
             deconv_sum = sum_filt
         else:
             deconv_diff = sg.convolve(diff_filt, [1, -1])
             deconv_sum = sg.convolve(sum_filt, [1, 1])
 
         lpc = .5 * (deconv_sum + deconv_diff)
         all_lpc[i] = lpc[:-1]
     return np.squeeze(all_lpc)
Exemplo n.º 5
0
    def synth(self, LPC, Pitch, Gain):
        stp = self.param.step
        Sn = np.array([])

        SniPrev = np.zeros(stp)
        stepInset = 0
        for step, pitch in enumerate(Pitch[:]):

            lpcCoefs = LPC[step]
            gain = float(Gain[step])
            stepLeftover = stp - stepInset
            Sni = np.array([])
            if pitch == 0:
                G = float(np.sqrt(1 / stp) * gain)
                Sni = np.append(Sni, self.gNoise(stepLeftover, G))
                stepInset = 0
            else:
                G = float(np.sqrt(pitch / stp) * gain)
                innerJumps = int(1 if pitch == 0 else np.ceil(stepLeftover /
                                                              pitch))
                spannedStep = 0
                Snisub = np.array([])
                for ij in range(innerJumps):
                    Snisubi = self.glutealPulse(pitch, G)
                    Snisub = np.append(Snisub, Snisubi)
                    spannedStep += pitch
                Sni = np.append(Sni, Snisub)
                stepInset = spannedStep - stepLeftover

            Sni = self.linearPredictor(lpcCoefs, Sni, SniPrev)
            SniPrev = Sni
            Sn = np.append(Sn, np.array(Sni))

        return Sn
Exemplo n.º 6
0
 def glutealPulse(self, pitch, gain=1):
     N0 = np.int(pitch)
     Nop = np.int(N0 * 2 / 3)
     pulse = np.zeros(N0)
     for n in range(Nop):
         pulse[n] = ((2 * Nop - 1) * n - 3 * n**2) / (Nop**2 - 3 * Nop + 2)
     return pulse * gain
Exemplo n.º 7
0
    def run(self, save=1):
        stp = self.param.step
        Sn = np.array([])

        SniPrev = np.zeros(self.param.step)
        stepInset = 0
        for step, pitch in enumerate(self.data.pitch[:]):

            lpcCoefs = self.data.lpc[step]
            gain = float(self.data.gain[step])
            stepLeftover = stp - stepInset
            Sni = np.array([])
            if pitch == 0:
                G = float(np.sqrt(1 / self.param.step) * gain)
                Sni = np.append(Sni, self.gNoise(stepLeftover, G))
                stepInset = 0
            else:
                G = float(np.sqrt(pitch / self.param.step) * gain)
                innerJumps = int(1 if pitch == 0 else np.ceil(stepLeftover /
                                                              pitch))
                spannedStep = 0
                Snisub = np.array([])
                for ij in range(innerJumps):
                    Snisubi = self.glutealPulse(pitch, G)
                    Snisub = np.append(Snisub, Snisubi)
                    spannedStep += pitch
                Sni = np.append(Sni, Snisub)
                stepInset = spannedStep - stepLeftover

            Sni = self.linearPredictor(lpcCoefs, Sni, SniPrev)

            SniPrev = Sni

            Sn = np.append(Sn, np.array(Sni))

            if (step in self.pc.stepInto3 or step in self.pc.stepIntoAll):
                self.p.prnt(2, str(step) + "------------------ start", 1)
                self.p.prnt(4, str("In Third Cycle"), 1)
                self.p.plot([(Sn, 'Sn', 'b', 0),
                             (Sni, 'Sni', 'r', len(Sn) - len(Sni)),
                             (self.data.raw[:len(Sn)] * 1, 'raw*0.3', 'c', 0)],
                            0)
                prev = 3
                since = len(Sn) - len(Sni) * prev
                since = 0 if since < 0 else since
                self.p.plot(
                    [(self.data.raw[since:len(Sn)] * 2, 'raw*2', 'c', since),
                     (Sn[since:len(Sn) - len(Sni)], 'Sn', 'b', since),
                     (Sni, 'Sni', 'r', len(Sn) - len(Sni))], 0)

                if self.pc.stop3:
                    input("   ...")

        self.syntesized = Sn

        if save:
            self.pickle.SaveData(self.data)
            self.pickle.save('syntesized', self.syntesized)
Exemplo n.º 8
0
def yearly_pattern():
    '''
    Definition of a yearly pattern of weekends and weekdays, in case some appliances have specific wd/we behaviour
    '''
    #Yearly behaviour pattern
    Year_behaviour = np.zeros(365)
    Year_behaviour[5:365:7] = 1
    Year_behaviour[6:365:7] = 1
    
    return(Year_behaviour)
Exemplo n.º 9
0
 def getDistanceMapOfAc(self, sR, sT):
     R = len(sR)
     T = len(sT)
     D = np.zeros([R, T])
     for r in range(R):
         for t in range(T):
             tMin = (max(r * (T / (R * 2)), (r - R * 0.5) * (2 * T / R)))
             tMax = (min(r * (2 * T / R), (r + R) * (T / 2 / R)))
             if not (tMin <= t + 1 and t - 1 <= tMax):
                 D[r, t] = np.Inf
             else:
                 D[r, t] = (sum((sT[t] - sR[r])**2)**(0.5))
     return D
Exemplo n.º 10
0
 def getDistanceMap(self, sR, sT):
     R = len(sR)
     T = len(sT)
     D = np.zeros([R, T])
     for r in range(R):
         for t in range(T):
             tMin = (max(r * (T / (R * 2)), (r - R * 0.5) * (2 * T / R)))
             tMax = (min(r * (2 * T / R), (r + R) * (T / 2 / R)))
             if tMin <= t and t <= tMax:
                 D[r, t] = np.sqrt((sR[r] - sT[t])**2)
             else:
                 D[r, t] = np.Inf
     return D
Exemplo n.º 11
0
    def getDistanceRoute(self, expD):
        target = expD.shape
        Route = np.zeros(expD.shape)
        expDRoute = np.array(expD)

        baseline = 0.5
        pos = np.array([0, 0])
        dist = 0
        Route[pos[0], pos[1]] = baseline
        step = 0

        while ((target[0] - 1) - pos[0] + (target[0] - 1) - pos[0]) != 0:
            around = expD[pos[0]:pos[0] + 2, pos[1]:pos[1] + 2]
            pos, dist, delta = self.stepOne(dist, pos, around)
            step = step + 1
            Route[pos[0], pos[1]] = baseline + delta
            expDRoute[pos[0], pos[1]] = expDRoute[pos[0], pos[1]] + 3
        globalDist = np.inf
        if 0 < step:
            globalDist = dist / step

        return globalDist, Route, expDRoute
Exemplo n.º 12
0
 def getExpandedDistanceMap(self, D):
     eD = np.zeros(np.array(D.shape) + 1) + np.Inf
     eD[1:, 1:] = D
     eD[0, 0] = 0
     return eD
Exemplo n.º 13
0
    def run(self, save=1):
        stp =  self.param.step
 

        for step, idx in enumerate(range(0,len(self.data.raw),stp)):
            pitch = self.data.pitch[step]
            trama  = self.data.raw[idx:idx+self.param.pf]            
            h = np.zeros(self.param.pf)

            tramaHp = trama
            if pitch:
                tramaHp = self.voicedPreprocesing(trama)
            ham = np.hamming(len(tramaHp))
            tramaHpHam = tramaHp*ham
            tramaHpHamAc = self.m.autocorrelation(tramaHpHam)
            tramaHpHamAcP=tramaHpHamAc[0:self.param.p]
            lpcCoefs = self.calculateLpcCoefs(tramaHpHamAcP)
            energy = self.data.power[step]*self.param.step

            G = self.calculateGain(trama, energy, lpcCoefs)

            for it, t in enumerate(trama): 
                if it<self.param.p-1:
                    h[it] = 0
                elif it==self.param.p:
                    h[it] = G
                else:
                    for ic, c in enumerate(lpcCoefs):
                        h[it] -=  c*h[it-ic-1]
                        
            hShift = np.append( h[self.param.p-1:], np.zeros(self.param.p-1))
            hShiftFft = np.fft.fft(hShift)
            trFft = np.fft.fft(tramaHp)

            
            hShiftAc = self.m.autocorrelation(hShift)

            self.data.gain.append(G)    
            if step==0:
                self.data.lpc = lpcCoefs
            else:             
                self.data.lpc = np.vstack([self.data.lpc, lpcCoefs])
            
            
            if (step in self.pc.stepInto2 or step in self.pc.stepIntoAll):
                self.p.prnt(2, str(step)+"------------------ start", 1)
                self.p.prnt(4, str("In Second Cycle"), 1)
                self.p.prnt(6, "Current voice pitch: " +str(self.data.pitch[step]), 1)
                self.p.plot([(self.data.raw, 'speech', 'y', 0),(trama, 'trama', 'r',  idx)])

                ptrFft = 20*np.log10(trFft[0:int(len(trFft)/2)])
                phShiftFft = 20*np.log10(hShiftFft[0:int(len(hShiftFft)/2)])
                self.p.plot([(tramaHp, 'trama - high pass', 'b',  0)])
                self.p.plot([(tramaHpHam, 'trama - hamming', 'b',  0)])
                self.p.plot([(tramaHpHamAc, 'trama - auto correlation', 'b',  0),(tramaHpHamAc[0:self.param.p], 'trama - auto correlation (p='+str(self.param.p)+")", 'r',  0)])
                self.p.plot([(h, 'h', 'm',  0)])
                self.p.plot([(hShift, 'hShift', 'm',  0)])
                self.p.plot([(ptrFft, 'trFft dB', 'b',  0),(phShiftFft, 'phShiftFft dB', 'r',  0)],0)
                self.p.plot([(hShiftAc[:30], 'hShiftAc 30', 'r',  0),(tramaHpHamAc[:30], 'tramaHpHamAc 30', 'b',  0)],0)

                self.p.plot([(hShiftAc[:self.param.p], 'hShiftAc p', 'r',  0),(tramaHpHamAc[:self.param.p], 'tramaHpHamAc p', 'b',  0)],0)

                self.p.prnt(2, str(step)+"------------------ end", 1)

                if self.pc.stop2:
                    input("   ...")
                    
        self.data.pitch = np.mat(self.data.pitch).T
        self.data.gain = np.mat(self.data.gain).T
        

        
        if save:
            self.pickle.SaveData(self.data)