Beispiel #1
0
def _clean_use_thresh(Y, thld, wavelet):
    LL = np.int32(np.floor(np.log2(len(Y))))
    wres = swt(Y, wavelet, level=LL)
    wres = [list(wres[i]) for i in range(len(wres))]
    # xh = HardTh(xh, thld);
    for i in xrange(len(wres)):
        wres[i][1] = (np.abs(wres[i][1]) > thld) * wres[i][1]
    # xd = mirdwt(xl,xh,h,LL);
    xd = iswt(wres, wavelet)
    # xn = Y - xd;
    xn = Y - xd
    # return the cleaned data
    return xn
Beispiel #2
0
def _clean_use_thresh(Y,thld,wavelet):
    LL = np.int32(np.floor(np.log2(len(Y))))
    wres = swt(Y,wavelet,level=LL)
    wres = [list(wres[i]) for i in range(len(wres))]
    # xh = HardTh(xh, thld);
    for i in xrange(len(wres)):
        wres[i][1] = (np.abs(wres[i][1]) > thld) * wres[i][1]
    # xd = mirdwt(xl,xh,h,LL);
    xd = iswt(wres, wavelet)
    # xn = Y - xd;
    xn = Y - xd
    # return the cleaned data
    return xn
Beispiel #3
0
def _clean_find_thresh(Y, Kthr, wavelet, L):
    # init
    xn = None
    thld = 0.0

    N = len(Y)

    # find the outliers
    # need to replace this with blink-finding code

    if False:
        # Sig = median(abs(Y)/0.6745);
        #Sig = np.median(np.abs(Y)/0.6745)
        #Sig = np.median(np.abs(icaEEG[Comp[c],pure_range[0]:pure_range[1]])/0.6745)
        Sig = np.median(np.abs(Y) / 0.6745)
        # Thr = 4*Sig;
        Thr = 3 * Sig
        # idx = find(abs(Y) > Thr);
        idx = np.nonzero(np.abs(Y) > Thr)[0]
        # idx_ext = zeros(1,length(idx)*(2*L+1));
        idx_ext = np.zeros(len(idx) * (2 * L + 1), dtype=np.int32)
        # for k=1:length(idx),
        #     idx_ext((2*L+1)*(k-1)+1:(2*L+1)*k) = [idx(k)-L:idx(k)+L];
        # end
        for k in xrange(len(idx)):
            idx_ext[(2 * L + 1) * (k):(2 * L + 1) * (k + 1) - 1] = np.arange(
                idx[k] - L, idx[k] + L)
        # id_noise=setdiff((1:N), idx_ext);
        id_noise = np.setdiff1d(range(N), idx_ext)
        # id_artef=setdiff((1:N), id_noise);
        id_artef = np.setdiff1d(range(N), id_noise)
    else:
        id_artef, id_noise = find_blinks(Y, L)

    # make sure it's not all noise or artifact
    print len(id_artef), len(id_noise)

    # if isempty(id_artef),
    #     disp(['The component #' num2str(Comp(c)) ' has passed unchanged']);
    #     continue;
    # end
    if len(id_artef) == 0:
        #sys.stdout.write("passed unchanged\n")
        #sys.stdout.flush()
        return xn, thld
    # KK = 100;
    KK = 100.
    # LL = floor(log2(length(Y)));
    LL = np.int32(np.floor(np.log2(len(Y))))
    # [xl, xh] = mrdwt(Y, h, LL);
    wres = swt(Y, wavelet, level=LL)
    # make it editable
    wres = [list(wres[i]) for i in range(len(wres))]

    # start with a low high-pass threshold and zero out wavelet
    # components below that value, test to see if the artifacts look
    # like the noise, if not, step up the threshold. At some point it
    # should stop, but perhaps not after removing a good bit of
    # signal.

    # while KK > Kthr,
    #     thld = thld + 0.5;
    #     xh = HardTh(xh, thld); % x = (abs(y) > thld).*y;
    #     xd = mirdwt(xl,xh,h,LL);
    #     xn = Y - xd;
    #     cn=corrcoef(Y(id_noise),xn(id_noise));
    #     ca=corrcoef(Y(id_artef),xd(id_artef));
    #     KK = ca(1,2)/cn(1,2);
    # end
    # thld = 3.6;
    # not sure where this 3.6 number came from, so I'm dropping it down to get
    # more low-freq cleaning
    thld = 1.1  #3.6
    while KK > Kthr:
        # update what's going on
        #sys.stdout.write('.')
        #sys.stdout.flush()
        # bump up the thresh
        thld += 0.5
        # zero out everything below threshold in each wavelet coef
        for i in xrange(len(wres)):
            wres[i][1] = (np.abs(wres[i][1]) > thld) * wres[i][1]
        # invert the wavelet back
        xd = iswt(wres, wavelet)
        # check if clean based on the ratio of correlations for noise
        # and artifact data
        xn = Y - xd
        # cn measures the correlation between the cleaned and original
        # data in the non-artifactual regions
        cn = np.corrcoef(Y[id_noise], xn[id_noise])[0, 1]
        # ca measures the corr b/t the signal removed from the
        # artifacts and the original artifacts
        ca = np.corrcoef(Y[id_artef], xd[id_artef])[0, 1]
        # must not go negative, it should just be a small positive
        # number if that happens
        if cn <= 0.0:
            cn = .000001
        if ca <= 0.0:
            ca = .000001
        # we want the ratio of the bad things getting cleaned to the
        # good things sticking around to be small, ideally both very
        # close to 1.0
        KK = ca / cn
        sys.stdout.write('(%.2f,%.2f,%.2f) ' % (ca, cn, KK))
        sys.stdout.flush()
    # return the cleaned data and the thresh
    return xn, thld
Beispiel #4
0
def _clean_find_thresh(Y,Kthr,wavelet,L):
    # init
    xn = None
    thld = 0.0

    N = len(Y)
    
    # find the outliers
    # need to replace this with blink-finding code

    if False:
        # Sig = median(abs(Y)/0.6745);
        #Sig = np.median(np.abs(Y)/0.6745)
        #Sig = np.median(np.abs(icaEEG[Comp[c],pure_range[0]:pure_range[1]])/0.6745)
        Sig = np.median(np.abs(Y)/0.6745)
        # Thr = 4*Sig;
        Thr = 3*Sig
        # idx = find(abs(Y) > Thr);
        idx = np.nonzero(np.abs(Y) > Thr)[0]
        # idx_ext = zeros(1,length(idx)*(2*L+1));
        idx_ext = np.zeros(len(idx)*(2*L+1), dtype=np.int32)
        # for k=1:length(idx),
        #     idx_ext((2*L+1)*(k-1)+1:(2*L+1)*k) = [idx(k)-L:idx(k)+L];
        # end
        for k in xrange(len(idx)):
            idx_ext[(2*L+1)*(k):(2*L+1)*(k+1)-1] = np.arange(idx[k]-L,idx[k]+L)
        # id_noise=setdiff((1:N), idx_ext);
        id_noise = np.setdiff1d(range(N), idx_ext)
        # id_artef=setdiff((1:N), id_noise);
        id_artef = np.setdiff1d(range(N), id_noise)
    else:
        id_artef,id_noise = find_blinks(Y,L)

    # make sure it's not all noise or artifact
    print len(id_artef),len(id_noise)

    # if isempty(id_artef),
    #     disp(['The component #' num2str(Comp(c)) ' has passed unchanged']);
    #     continue;
    # end
    if len(id_artef) == 0:
        #sys.stdout.write("passed unchanged\n")
        #sys.stdout.flush()
        return xn, thld
    # KK = 100;
    KK = 100.
    # LL = floor(log2(length(Y)));
    LL = np.int32(np.floor(np.log2(len(Y))))
    # [xl, xh] = mrdwt(Y, h, LL);
    wres = swt(Y,wavelet,level=LL)
    # make it editable
    wres = [list(wres[i]) for i in range(len(wres))]

    # start with a low high-pass threshold and zero out wavelet
    # components below that value, test to see if the artifacts look
    # like the noise, if not, step up the threshold. At some point it
    # should stop, but perhaps not after removing a good bit of
    # signal.

    # while KK > Kthr,
    #     thld = thld + 0.5;
    #     xh = HardTh(xh, thld); % x = (abs(y) > thld).*y;
    #     xd = mirdwt(xl,xh,h,LL); 
    #     xn = Y - xd;
    #     cn=corrcoef(Y(id_noise),xn(id_noise));
    #     ca=corrcoef(Y(id_artef),xd(id_artef));
    #     KK = ca(1,2)/cn(1,2);   
    # end
    # thld = 3.6;
    # not sure where this 3.6 number came from, so I'm dropping it down to get
    # more low-freq cleaning
    thld = 1.1 #3.6
    while KK > Kthr:
        # update what's going on
        #sys.stdout.write('.')
        #sys.stdout.flush()
        # bump up the thresh
        thld += 0.5
        # zero out everything below threshold in each wavelet coef
        for i in xrange(len(wres)):
            wres[i][1] = (np.abs(wres[i][1]) > thld) * wres[i][1]
        # invert the wavelet back
        xd = iswt(wres, wavelet)
        # check if clean based on the ratio of correlations for noise
        # and artifact data
        xn = Y-xd
        # cn measures the correlation between the cleaned and original
        # data in the non-artifactual regions
        cn = np.corrcoef(Y[id_noise],xn[id_noise])[0,1]
        # ca measures the corr b/t the signal removed from the
        # artifacts and the original artifacts
        ca = np.corrcoef(Y[id_artef],xd[id_artef])[0,1]
        # must not go negative, it should just be a small positive
        # number if that happens
        if cn <= 0.0:
            cn = .000001
        if ca <= 0.0:
            ca = .000001
        # we want the ratio of the bad things getting cleaned to the
        # good things sticking around to be small, ideally both very
        # close to 1.0
        KK = ca/cn
        sys.stdout.write('(%.2f,%.2f,%.2f) '%(ca,cn,KK))
        sys.stdout.flush()
    # return the cleaned data and the thresh
    return xn, thld
Beispiel #5
0
def _clean_find_thresh(Y,Kthr,wavelet,L):
    # init
    xn = None
    thld = 0.0

    N = len(Y)
    
    # Sig = median(abs(Y)/0.6745);
    #Sig = np.median(np.abs(Y)/0.6745)
    #Sig = np.median(np.abs(icaEEG[Comp[c],pure_range[0]:pure_range[1]])/0.6745)
    Sig = np.median(np.abs(Y)/0.6745)
    # Thr = 4*Sig;
    Thr = 4*Sig
    # idx = find(abs(Y) > Thr);
    idx = np.nonzero(np.abs(Y) > Thr)[0]
    # idx_ext = zeros(1,length(idx)*(2*L+1));
    idx_ext = np.zeros(len(idx)*(2*L+1), dtype=np.int32)
    # for k=1:length(idx),
    #     idx_ext((2*L+1)*(k-1)+1:(2*L+1)*k) = [idx(k)-L:idx(k)+L];
    # end
    for k in xrange(len(idx)):
        idx_ext[(2*L+1)*(k):(2*L+1)*(k+1)-1] = np.arange(idx[k]-L,idx[k]+L)
    # id_noise=setdiff((1:N), idx_ext);
    id_noise = np.setdiff1d(range(N), idx_ext)
    # id_artef=setdiff((1:N), id_noise);
    id_artef = np.setdiff1d(range(N), id_noise)
    # if isempty(id_artef),
    #     disp(['The component #' num2str(Comp(c)) ' has passed unchanged']);
    #     continue;
    # end
    if len(id_artef) == 0:
        #sys.stdout.write("passed unchanged\n")
        #sys.stdout.flush()
        return xn, thld
    # thld = 3.6;
    # not sure where this 3.6 number came from, so I'm dropping it down
    thld = .1 #3.6
    # KK = 100;
    KK = 100.
    # LL = floor(log2(length(Y)));
    LL = np.int32(np.floor(np.log2(len(Y))))
    # [xl, xh] = mrdwt(Y, h, LL);
    wres = swt(Y,wavelet,level=LL)
    # make it editable
    wres = [list(wres[i]) for i in range(len(wres))]
    # while KK > Kthr,
    #     thld = thld + 0.5;
    #     xh = HardTh(xh, thld); % x = (abs(y) > thld).*y;
    #     xd = mirdwt(xl,xh,h,LL); 
    #     xn = Y - xd;
    #     cn=corrcoef(Y(id_noise),xn(id_noise));
    #     ca=corrcoef(Y(id_artef),xd(id_artef));
    #     KK = ca(1,2)/cn(1,2);   
    # end
    while KK > Kthr:
        sys.stdout.write('.')
        sys.stdout.flush()
        for i in xrange(len(wres)):
            wres[i][1] = (np.abs(wres[i][1]) > thld) * wres[i][1]
        xd = iswt(wres, wavelet)
        xn = Y-xd
        cn = np.corrcoef(Y[id_noise],xn[id_noise])
        ca = np.corrcoef(Y[id_artef],xd[id_artef])
        KK = ca[0,1]/cn[0,1]
        thld += 0.5
    # return the cleaned data and the thresh
    return xn, thld
Beispiel #6
0
def _clean_find_thresh(Y, Kthr, wavelet, L):
    # init
    xn = None
    thld = 0.0

    N = len(Y)

    # Sig = median(abs(Y)/0.6745);
    #Sig = np.median(np.abs(Y)/0.6745)
    #Sig = np.median(np.abs(icaEEG[Comp[c],pure_range[0]:pure_range[1]])/0.6745)
    Sig = np.median(np.abs(Y) / 0.6745)
    # Thr = 4*Sig;
    Thr = 4 * Sig
    # idx = find(abs(Y) > Thr);
    idx = np.nonzero(np.abs(Y) > Thr)[0]
    # idx_ext = zeros(1,length(idx)*(2*L+1));
    idx_ext = np.zeros(len(idx) * (2 * L + 1), dtype=np.int32)
    # for k=1:length(idx),
    #     idx_ext((2*L+1)*(k-1)+1:(2*L+1)*k) = [idx(k)-L:idx(k)+L];
    # end
    for k in xrange(len(idx)):
        idx_ext[(2 * L + 1) * (k):(2 * L + 1) * (k + 1) - 1] = np.arange(
            idx[k] - L, idx[k] + L)
    # id_noise=setdiff((1:N), idx_ext);
    id_noise = np.setdiff1d(range(N), idx_ext)
    # id_artef=setdiff((1:N), id_noise);
    id_artef = np.setdiff1d(range(N), id_noise)
    # if isempty(id_artef),
    #     disp(['The component #' num2str(Comp(c)) ' has passed unchanged']);
    #     continue;
    # end
    if len(id_artef) == 0:
        #sys.stdout.write("passed unchanged\n")
        #sys.stdout.flush()
        return xn, thld
    # thld = 3.6;
    # not sure where this 3.6 number came from, so I'm dropping it down
    thld = .1  #3.6
    # KK = 100;
    KK = 100.
    # LL = floor(log2(length(Y)));
    LL = np.int32(np.floor(np.log2(len(Y))))
    # [xl, xh] = mrdwt(Y, h, LL);
    wres = swt(Y, wavelet, level=LL)
    # make it editable
    wres = [list(wres[i]) for i in range(len(wres))]
    # while KK > Kthr,
    #     thld = thld + 0.5;
    #     xh = HardTh(xh, thld); % x = (abs(y) > thld).*y;
    #     xd = mirdwt(xl,xh,h,LL);
    #     xn = Y - xd;
    #     cn=corrcoef(Y(id_noise),xn(id_noise));
    #     ca=corrcoef(Y(id_artef),xd(id_artef));
    #     KK = ca(1,2)/cn(1,2);
    # end
    while KK > Kthr:
        sys.stdout.write('.')
        sys.stdout.flush()
        for i in xrange(len(wres)):
            wres[i][1] = (np.abs(wres[i][1]) > thld) * wres[i][1]
        xd = iswt(wres, wavelet)
        xn = Y - xd
        cn = np.corrcoef(Y[id_noise], xn[id_noise])
        ca = np.corrcoef(Y[id_artef], xd[id_artef])
        KK = ca[0, 1] / cn[0, 1]
        thld += 0.5
    # return the cleaned data and the thresh
    return xn, thld