def predlin(s,p,w):
    """
    Function that computes de linear prediction using lpc for a given signal
    """
    x = s*w
    
    lpc,e = spectrum.lpc(x,N = p)
    #E = np.sqrt(np.sum(x**2))
    ar,sigma,lt = spectrum.aryule(x,p)
    
    
    plt.subplot(3,1,1)
    plt.plot(s)
    plt.title("Speech signal")
    xx = np.arange(0,len(w))
    plt.plot(xx,w*np.max(s))

    plt.subplot(312)
    plt.plot(x)
    plt.title("Windowed signal")
    
    plt.subplot(313)
    S_x = 20*np.log10(np.abs(np.fft.fft(x,2*len(w))))
    plt.plot(S_x[0:len(w)])
    ff,h = scipy.signal.freqz(1,ar,len(w),whole=False)
    h2 = spectrum.arma2psd(ar,NFFT=len(w)*2)
    plt.plot(20*np.log10(np.abs(h2[0:len(w)])))
    def __init__(self,
                 xw,
                 dim_a=20,
                 dim_b=20,
                 max_lag=100,
                 NFFT=4096,
                 sr=44100,
                 Flag_Show=True):
        #
        self.xw = xw.copy()
        self.sr = sr  # sampling rate
        self.NFFT = NFFT
        self.Flag_Show = Flag_Show
        # ARMA model, a and b
        self.a, self.b, self.rho = spectrum.arma_estimate(
            np.array(self.xw), dim_a, dim_b, max_lag)
        if self.Flag_Show:
            print('a=', self.a)
            print('b=', self.b)
            print('White noise variance estimate (rho) ', self.rho)

        # rho_1=1.0  # if force to use 1 as rho
        self.psd = spectrum.arma2psd(A=self.a,
                                     B=self.b,
                                     rho=self.rho,
                                     NFFT=self.NFFT,
                                     norm=False)
        self.psd2 = self.psd[0:int(len(self.psd) / 2)]  # get half side
        self.get_peak()

        # other ways
        self.psd2_ar = None
        self.psd_welch = None
Ejemplo n.º 3
0
 def GetBurg(self, X):
     import spectrum
     AR, rho, ref = spectrum.arburg(X, order=20)
     psd = spectrum.arma2psd(AR, rho=rho, NFFT=4096)
     psd = psd[len(psd):len(psd) / 2:-1]
     p = 10 * np.log(abs(psd) * 2. / (2. * np.pi))
     F = spectrum.linspace(0, 80, len(p))
     return p, F
    def ar_model(self, ):  # another way
        # AR model using the Yule-Walker (autocorrelation) method
        self.ar, self.var, self.reflec = spectrum.aryule(
            np.array(self.xw), 30, allow_singularity=True)
        if self.Flag_Show:
            print('a=', self.ar)

        self.psd_ar = spectrum.arma2psd(A=self.ar, NFFT=self.NFFT, norm=False)
        self.psd2_ar = self.psd_ar[0:int(len(self.psd_ar) /
                                         2)]  # get half side
Ejemplo n.º 5
0
    def __call__(self):
        from spectrum import arma2psd
        ar, e = modcovar(self.data, self.ar_order)
        self.ar = ar
        psd = arma2psd(A=ar, T=self.sampling, NFFT=self.NFFT)

        if self.datatype == 'real':
            if self.NFFT % 2 == 0:
                newpsd = psd[0:int(self.NFFT / 2 + 1)] * 2
            else:
                newpsd = psd[0:int((self.NFFT + 1) / 2)] * 2
            self.psd = newpsd
        else:
            self.psd = psd
        if self.scale_by_freq is True:
            self.scale()
Ejemplo n.º 6
0
    def __call__(self):
        from spectrum import arma2psd
        ar, _e = arcovar(self.data, self.ar_order)
        self.ar = ar
        psd = arma2psd(A=ar, T=self.sampling, NFFT=self.NFFT)

        if self.datatype == 'real':
            if self.NFFT % 2 == 0:
                newpsd = psd[0:int(self.NFFT/2+1)] * 2
            else:
                newpsd = psd[0:int((self.NFFT+1)/2)] * 2
            self.psd = newpsd
        else:
            self.psd = psd
        if self.scale_by_freq is True:
            self.scale()
Ejemplo n.º 7
0
    def __call__(self):
        from spectrum import arma2psd
        ar, e = modcovar(self.data, self.ar_order)
        self.ar = ar
        psd = arma2psd(A=ar, T=self.sampling, NFFT=self.NFFT)

        if self.datatype == 'real':
            #from .tools import twosided_2_onesided
            #newpsd  = twosided_2_onesided(psd)
            newpsd  = psd[0:int(self.NFFT//2)] * 2
            # TODO: check the last value is correct or required ?
            newpsd = np.append(newpsd, psd[int(self.NFFT//2)]*2)
            self.psd = newpsd
        else:
            self.psd = psd
        if self.scale_by_freq is True:
            self.scale()
Ejemplo n.º 8
0
 def __call__(self):
     from spectrum import arma2psd
     ar, _e = arcovar(self.data, self.ar_order)
     self.ar = ar
     psd = arma2psd(A=ar, T=self.sampling, NFFT=self.NFFT)
     
     if self.datatype == 'real':
         from tools import twosided_2_onesided
         newpsd  = twosided_2_onesided(psd)
         #\psd[0:self.NFFT/2]*2
         #newpsd[0] /= 2.
         #newpsd = numpy.append(newpsd, psd[-1])
         self.psd = newpsd
     else:
         self.psd = psd
     if self.scale_by_freq is True:
         self.scale()
Ejemplo n.º 9
0
    def __call__(self):
        from spectrum import arma2psd
        ar, e = modcovar(self.data, self.ar_order)
        self.ar = ar
        psd = arma2psd(A=ar, T=self.sampling, NFFT=self.NFFT)

        if self.datatype == 'real':
            #from .tools import twosided_2_onesided
            #newpsd  = twosided_2_onesided(psd)
            newpsd = psd[0:int(self.NFFT // 2)] * 2
            # TODO: check the last value is correct or required ?
            newpsd = np.append(newpsd, psd[int(self.NFFT // 2)] * 2)
            self.psd = newpsd
        else:
            self.psd = psd
        if self.scale_by_freq is True:
            self.scale()
Ejemplo n.º 10
0
    def __call__(self):
        from spectrum import arma2psd
        ar, e = modcovar(self.data, self.ar_order)
        self.ar = ar
        psd = arma2psd(A=ar, T=self.sampling, NFFT=self.NFFT)

        if self.datatype == 'real':
            from .tools import twosided_2_onesided
            newpsd = twosided_2_onesided(psd)
            #\psd[0:self.NFFT/2]*2
            #newpsd[0] /= 2.
            #newpsd = numpy.append(newpsd, psd[-1])
            self.psd = newpsd
        else:
            self.psd = psd
        if self.scale_by_freq is True:
            self.scale()
Ejemplo n.º 11
0
import scipy as sp
import scipy.signal as signal
import matplotlib.pyplot as plt
import spectrum

# ### Simulo proceso AR-4

# In[2]:

M = 10000
W = np.random.randn(M)
Y = signal.lfilter([1], [1, 0.3544, 0.3508, 0.1736, 0.2401], W)

# In[3]:

psd = spectrum.arma2psd([0.3544, 0.3508, 0.1736, 0.2401], NFFT=5000)

# In[38]:


def make_plot(Pxx_den, Pxx_den_w, psd, n):
    plt.figure(figsize=(16, 9))
    plt.semilogy(f, (Pxx_den), label="Periodograma")
    plt.semilogy(f_w, (Pxx_den_w), label="Welch")
    plt.semilogy(np.linspace(0, 1, 5000), (psd), label="PSD")
    plt.grid(True, which="both", alpha=0.3)
    plt.ylabel("Densidad espectral [dB]", size="xx-large")
    plt.xlabel("Frecuencia [rad]", size="xx-large")
    plt.title(
        f"Periodograma, estimador de Welch y PSD verdadera para AR-4 (n={n})",
        size="xx-large")
Ejemplo n.º 12
0
def ar2psd( ar, n ):
    '''Compute PSD from a collection of ar models'''
    if ar.ndim > 1:
        return np.asarray([spec.arma2psd(ar[1:,c], NFFT=2*(n-1))[:n] for c in range(ar.shape[1])]).T
    else:
        return spec.arma2psd(ar[1:], NFFT=2*(n-1))[:n]
Ejemplo n.º 13
0
def calculateFDindexes(RR,  Finterp):
    
    def power(spec,freq,fmin,fmax):
        #returns power in band
        band = np.array([spec[i] for i in range(len(spec)) if freq[i] >= fmin and freq[i]<fmax])
        powerinband = np.sum(band)/len(spec)
        return powerinband
   
    def InterpolateRR(RR, Finterp):
        # returns cubic spline interpolated array with sample rate = Finterp
        step=1/Finterp 
        BT=np.cumsum(RR) 
        xmin=BT[0]
        xmax=BT[-1]
        BT = np.insert(BT,0,0)
        BT=np.append(BT, BT[-1]+1)
        RR = np.insert(RR,0,0)
        RR=np.append(RR, RR[-1])
        
        tck = interpolate.splrep(BT,RR)
        BT_interp = np.arange(xmin,xmax,step)
        RR_interp = interpolate.splev(BT_interp,  tck)
        return RR_interp,  BT_interp
    
    RR=RR/1000 #RR in seconds
    RR_interp, BT_interp=InterpolateRR(RR, Finterp)
    RR_interp=RR_interp-np.mean(RR_interp)

    freqs=np.arange(0, 2, 0.0001)
    
    # calculates AR coefficients
    AR, P, k = spct.arburg(RR_interp*1000, 16) #burg
    
    # estimates PSD from AR coefficients
    spec = spct.arma2psd(AR,  T=0.25, NFFT=2*len(freqs)) # pectrum estimation
    spec = spec[0:len(spec)/2]   
    
    # WELCH psd estimation
    
    # calculates power in different bands
    VLF=power(spec,freqs,0,0.04)
    LF=power(spec,freqs,0.04,0.15)
    HF=power(spec,freqs,0.15,0.4)
    Total=power(spec,freqs,0,2)
    LFHF = LF/HF
    nVLF=VLF/Total # Normalized
    nLF=LF/Total
    nHF=HF/Total
    
    #NormalizedHF HFNormal

    LFn=LF/(HF+LF)
    HFn=HF/(HF+LF)
    Power = [VLF, HF, LF]
    
    Power_Ratio= Power/sum(Power)
#    Power_Ratio=spec/sum(spec) # uncomment to calculate Spectral Entropy using all frequencies
    Spectral_Entropy = 0
    lenPower=0 # tengo conto delle bande che ho utilizzato
    for i in xrange(0, len(Power_Ratio)):
        if Power_Ratio[i]>0: # potrei avere VLF=0
            Spectral_Entropy += Power_Ratio[i] * np.log(Power_Ratio[i])
            lenPower +=1
    Spectral_Entropy /= np.log(lenPower) #al posto di len(Power_Ratio) perche' magari non ho usato VLF
    
    labels= np.array(['VLF', 'LF', 'HF', 'Total', 'nVLF', 'nLF', 'nHF', 'LFn', 'HFn', 'LFHF', 'SpecEn'],  dtype='S10')
    
    return [VLF, LF, HF, Total, nVLF, nLF, nHF, LFn, HFn, LFHF, Spectral_Entropy], labels
Ejemplo n.º 14
0
def create_all_psd():


    f = pylab.linspace(0, 1, 4096)
    pylab.clf()

    pylab.figure(figsize=(12,8))

    #MA 15 order
    b, rho = spectrum.ma(data, 15, 30)
    psd = spectrum.arma2psd(B=b, rho=rho)
    newpsd = tools.cshift(psd, len(psd)//2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='MA 15')

    #ARMA 15 order
    a, b, rho = spectrum.arma_estimate(data, 15,15, 30)
    psd = spectrum.arma2psd(A=a,B=b, rho=rho)
    newpsd = tools.cshift(psd, len(psd)//2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='ARMA 15,15')

    #yulewalker
    ar, P,c = spectrum.aryule(data, 15, norm='biased')
    psd = spectrum.arma2psd(A=ar, rho=P)
    newpsd = tools.cshift(psd, len(psd)//2) # switch positive and negative freq

    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='YuleWalker 15')

    #burg method
    ar, P,k = spectrum.arburg(data, order=15)
    psd = spectrum.arma2psd(A=ar, rho=P)
    newpsd = tools.cshift(psd, len(psd)//2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='Burg 15')

    #covar method
    af, pf, ab, pb, pv = spectrum.arcovar_marple(data, 15)
    psd = spectrum.arma2psd(A=af, B=ab, rho=pf)
    newpsd = tools.cshift(psd, len(psd)//2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='covar 15')

    #modcovar method
    a, p, pv = spectrum.modcovar_marple(data, 15)
    psd = spectrum.arma2psd(A=a)
    newpsd = tools.cshift(psd, len(psd)//2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='modcovar 15')

    #correlogram
    psd = spectrum.CORRELOGRAMPSD(data, data, lag=15)
    newpsd = tools.cshift(psd, len(psd)/2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='correlogram 15')

    #minvar
    psd = spectrum.minvar(data, 15)
    #newpsd = tools.cshift(psd, len(psd)/2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd/max(newpsd)), label='MINVAR 15')

    #music
    psd,db = spectrum.music(data, 15, 11)
    pylab.plot(f, 10 * pylab.log10(psd/max(psd)), '--',label='MUSIC 15')

    #ev music
    psd,db = spectrum.ev(data, 15, 11)
    pylab.plot(f, 10 * pylab.log10(psd/max(psd)), '--',label='EV 15')


    pylab.legend(loc='upper left', prop={'size':10}, ncol=2)
    pylab.ylim([-80,10])
    pylab.savefig('psd_all.png')
Ejemplo n.º 15
0
plt.plot(order, spectrum.AIC(len(last1second), rho, order), label='AIC')
index_min = np.argmin(spectrum.AIC(len(last1second), rho,
                                   order))  #pick minimum rho
plt.title(index_min)

'choose just 1 second of 1 channel'
for abba in totaltrials:  #for loop to do multiple second chunks
    choiceofsecond = 6
    last1second = channel1data[-500 * choiceofsecond:-500 *
                               (choiceofsecond - 1)]  #pick a second chunk
    last1second = last1second - mean(last1second)  #remove mean
    plot(last1second)

    'autoregressive spectral analysis'
    [ar, var, reflec] = spectrum.aryule(last1second, index_min, norm='biased')
    psd = spectrum.arma2psd(ar)  #power spectrum analysis
    freqvals = np.linspace(0, frequency / 2, len(psd) / 2)  #frequencies
    #plt.plot(freqvals,psd[0:round(len(psd)/2)],label='AR')
    #plt.axis([0,20,0,1500])

    'fourier transform'
    #ff = fft(channel1data)
    #freqvals = np.fft.fftfreq(len(channel1data), 1.0/frequency)
    #powerspectrumvals = abs(ff[0:round(len(ff)/2)])**2
    #figure()
    #plt.plot(freqvals[0:round(len(ff)/2)],powerspectrumvals,label='FFT')
    #plt.axis([0,10,0,0.01])
    #plt.legend()
    #plt.show()

    'pick frequency'
Ejemplo n.º 16
0
def test_covar():
    af, pf, ab, pb, pv = arcovar_marple(marple_data, 15)
    PSD = arma2psd(af)

    newpsd = cshift(PSD, len(PSD) // 2)  # switch positive and negative freq
    return newpsd
Ejemplo n.º 17
0
"""Generate json spectrum data."""
from spectrum import arma_estimate, arma2psd, marple_data
import json
with open('dummy.json', 'w') as f:
    ar, ma, rho = arma_estimate(marple_data, 15, 15, 30)
    psd = arma2psd(ar, ma, rho=rho, NFFT=4096)
    print(json.dumps(list(psd)), file=f)
Ejemplo n.º 18
0
def create_all_psd():

    f = pylab.linspace(0, 1, 4096)
    pylab.clf()

    pylab.figure(figsize=(12, 8))

    #MA 15 order
    b, rho = spectrum.ma(data, 15, 30)
    psd = spectrum.arma2psd(B=b, rho=rho)
    newpsd = tools.cshift(psd,
                          len(psd) // 2)  # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd / max(newpsd)), label='MA 15')

    #ARMA 15 order
    a, b, rho = spectrum.arma_estimate(data, 15, 15, 30)
    psd = spectrum.arma2psd(A=a, B=b, rho=rho)
    newpsd = tools.cshift(psd,
                          len(psd) // 2)  # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd / max(newpsd)), label='ARMA 15,15')

    #yulewalker
    ar, P, c = spectrum.aryule(data, 15, norm='biased')
    psd = spectrum.arma2psd(A=ar, rho=P)
    newpsd = tools.cshift(psd,
                          len(psd) // 2)  # switch positive and negative freq

    pylab.plot(f,
               10 * pylab.log10(newpsd / max(newpsd)),
               label='YuleWalker 15')

    #burg method
    ar, P, k = spectrum.arburg(data, order=15)
    psd = spectrum.arma2psd(A=ar, rho=P)
    newpsd = tools.cshift(psd,
                          len(psd) // 2)  # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd / max(newpsd)), label='Burg 15')

    #covar method
    af, pf, ab, pb, pv = spectrum.arcovar_marple(data, 15)
    psd = spectrum.arma2psd(A=af, B=ab, rho=pf)
    newpsd = tools.cshift(psd,
                          len(psd) // 2)  # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd / max(newpsd)), label='covar 15')

    #modcovar method
    a, p, pv = spectrum.modcovar_marple(data, 15)
    psd = spectrum.arma2psd(A=a)
    newpsd = tools.cshift(psd,
                          len(psd) // 2)  # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd / max(newpsd)), label='modcovar 15')

    #correlogram
    psd = spectrum.CORRELOGRAMPSD(data, data, lag=15)
    newpsd = tools.cshift(psd,
                          len(psd) / 2)  # switch positive and negative freq
    pylab.plot(f,
               10 * pylab.log10(newpsd / max(newpsd)),
               label='correlogram 15')

    #minvar
    psd = spectrum.minvar(data, 15)
    #newpsd = tools.cshift(psd, len(psd)/2) # switch positive and negative freq
    pylab.plot(f, 10 * pylab.log10(newpsd / max(newpsd)), label='MINVAR 15')

    #music
    psd, db = spectrum.music(data, 15, 11)
    pylab.plot(f, 10 * pylab.log10(psd / max(psd)), '--', label='MUSIC 15')

    #ev music
    psd, db = spectrum.ev(data, 15, 11)
    pylab.plot(f, 10 * pylab.log10(psd / max(psd)), '--', label='EV 15')

    pylab.legend(loc='upper left', prop={'size': 10}, ncol=2)
    pylab.ylim([-80, 10])
    pylab.savefig('psd_all.png')
Ejemplo n.º 19
0
def test_covar():
    af, pf, ab, pb, pv = arcovar_marple(marple_data, 15)
    PSD = arma2psd(af)

    newpsd = cshift(PSD, len(PSD)//2) # switch positive and negative freq
    return newpsd