예제 #1
0
def linear2mu(signal, mu=255):
    """
    -1 < signal < 1 
    """
    assert signal.max() <= 1 and signal.min() >= -1
    y = np.sign(signal) * np.log(1.0 + mu * np.abs(signal)) / np.log(mu + 1)
    return y
예제 #2
0
    def compute(self):
        signal = self.get_input("Input").get_array().squeeze()
        print "signal.shape = ", signal.shape
        lof = self.get_input("Low Freq")
        hif = self.get_input("High Freq")

        num_scalar_bins = self.get_input("Scalar Bins")

        num_pts = signal.size
        min_s = signal.min()
        max_s = signal.max()
        d = max_s - min_s
        print "Accumulating over " + str(num_pts) + " points"
        histo = numpy.zeros((num_scalar_bins,hif-lof))

        f_sig = scipy.fftpack.fftn(signal)

        for z in range(signal.shape[0]):
            for y in range(signal.shape[1]):
                for x in range(signal.shape[2]):
                    yray = f_sig[z,:,x].squeeze()
                    ty = self.stockwell(yray, lof, hif)
                    zray = f_sig[:,y,x].squeeze()
                    tz = self.stockwell(zray, lof, hif)
                    xray = f_sig[z,y,:].squeeze()
                    tx = self.stockwell(xray, lof, hif)

                    tx = tx[:,z].squeeze()
                    ty = ty[:,y].squeeze()
                    tz = tz[:,x].squeeze()

                    tx = tx * tx.conjugate()
                    ty = ty * ty.conjugate()
                    tz = tz * tz.conjugate()
                    
                    ar = tx.real + ty.real + tz.real
                    ar = ar / ar.sum()
                    
    
                    scalar = signal[z,y,x]
                    try:
                        bin = int((scalar - min_s) / d * float(num_scalar_bins-1.))
#                        dist[bin] += 1
#                        print bin, scalar, ar
                        sigma = self.force_get_input("Sigma")
                        if sigma:
                            ar = scipy.signal.gaussian(ar.size, sigma) * ar
                        histo[bin,:] += ar
                    except:
                        print "Cannot assign to bin: " + str(bin) +", scalar: " +str(scalar)
                        print "location = ", x, y, z
                        raise ModuleError("Cannot assign to bin: " + str(bin) +", scalar: " +str(scalar))
                    
#                print "done with y = ", y
            print "done with z = ", z

        out = NDArray()
        out.set_array(histo)
        self.set_output("Output", out)
예제 #3
0
    def compute(self):
        signal = self.getInputFromPort("Input").get_array()
        lof = self.getInputFromPort("Low Freq")
        hif = self.getInputFromPort("High Freq")

        num_pts = signal.size
        min_s = signal.min()
        max_s = signal.max()
        d = max_s - min_s
        print "Accumulating over " + str(num_pts) + " points"
        histo = numpy.zeros((512, hif - lof + 1))
        print "Histo: ", histo.shape
        dist = numpy.zeros(512)
        for z in range(signal.shape[2]):
            for y in range(signal.shape[1]):
                for x in range(signal.shape[0]):
                    sigx = signal[z, y, :]
                    sigy = signal[z, :, x]
                    tx = st.st(sigx, lof, hif)
                    ty = st.st(sigy, lof, hif)
                    sigz = signal[:, y, z]
                    tz = st.st(sigz, lof, hif)
                    tz = tz[:, x].squeeze()
                    tz = tz * tz.conjugate()
                    tx = tx[:, z].squeeze()
                    ty = ty[:, y].squeeze()
                    tx = tx * tx.conjugate()
                    ty = ty * ty.conjugate()

                    ar = tx.real + ty.real + tz.real
                    ar = ar / ar.sum()
                    #                    ar = ar.sum(axis=1)
                    #                    print "ar: ", ar.shape
                    #                    ar.shape = (ar.shape[0],1)
                    scalar = signal[z, y, x]
                    try:
                        bin = int((scalar - min_s) / d * 511.)
                        #                        dist[bin] += 1
                        #                        print bin, scalar, ar
                        sigma = self.forceGetInputFromPort("Sigma")
                        if sigma:
                            ar = scipy.signal.gaussian(ar.size, sigma) * ar
                        histo[bin, :] += ar
                    except:
                        print "Cannot assign to bin: " + str(
                            bin) + ", scalar: " + str(scalar)
                        print "location = ", x, y, z
                        raise ModuleError("Cannot assign to bin: " + str(bin) +
                                          ", scalar: " + str(scalar))


#                print "done with y = ", y
            print "done with z = ", z

        out = NDArray()
        out.set_array(histo)  # / dist)
        self.setResult("Output", out)
예제 #4
0
    def compute(self):
        signal = self.get_input("Input").get_array()
        lof = self.get_input("Low Freq")
        hif = self.get_input("High Freq")

        num_pts = signal.size
        min_s = signal.min()
        max_s = signal.max()
        d = max_s - min_s
        print "Accumulating over " + str(num_pts) + " points"
        histo = numpy.zeros((512,hif-lof+1))
        print "Histo: ",histo.shape
        dist = numpy.zeros(512)
        for z in range(signal.shape[2]):
            for y in range(signal.shape[1]):
                for x in range(signal.shape[0]):
                    sigx = signal[z,y,:]
                    sigy = signal[z,:,x]
                    tx = st.st(sigx, lof, hif)
                    ty = st.st(sigy, lof, hif)
                    sigz = signal[:,y,z]
                    tz = st.st(sigz, lof, hif)
                    tz = tz[:,x].squeeze()
                    tz = tz * tz.conjugate()
                    tx = tx[:,z].squeeze()
                    ty = ty[:,y].squeeze()
                    tx = tx * tx.conjugate()
                    ty = ty * ty.conjugate()

                    ar = tx.real + ty.real + tz.real
                    ar = ar / ar.sum()
#                    ar = ar.sum(axis=1)
#                    print "ar: ", ar.shape
#                    ar.shape = (ar.shape[0],1)
                    scalar = signal[z,y,x]
                    try:
                        bin = int((scalar - min_s) / d * 511.)
#                        dist[bin] += 1
#                        print bin, scalar, ar
                        sigma = self.force_get_input("Sigma")
                        if sigma:
                            ar = scipy.signal.gaussian(ar.size, sigma) * ar
                        histo[bin,:] += ar
                    except:
                        print "Cannot assign to bin: " + str(bin) +", scalar: " +str(scalar)
                        print "location = ", x, y, z
                        raise ModuleError("Cannot assign to bin: " + str(bin) +", scalar: " +str(scalar))
                    
#                print "done with y = ", y
            print "done with z = ", z

        out = NDArray()
        out.set_array(histo)# / dist)
        self.set_output("Output", out)
예제 #5
0
def normalize(signal):
    """Normalize signal between 0 and 1

    Args:
        signal (np.array/th.tensor): Signal to normalize

    Returns:
        np.array/th.tensor: Normalized signal
    """
    signal -= signal.min()
    signal /= signal.max()
    return signal
예제 #6
0
def waveform_shaded(signal: np.ndarray, fs: int, start=0, end=None):
    if end is None:
        end = len(signal) / fs
    ymargin_factor = 1.1
    x_range = [start, end]
    y_range = [ymargin_factor * signal.min(), ymargin_factor * signal.max()]
    t = np.linspace(start, end, num=len(signal))
    df = pd.DataFrame(data={'Time': t, 'Signal': signal})
    cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_width=1500)

    cols = ['Signal']
    aggs = OrderedDict((c, cvs.line(df, 'Time', c)) for c in cols)
    img = tf.shade(aggs['Signal'])
    arr = np.array(img)
    z = arr.tolist()
    dims = len(z[0]), len(z)

    x = np.linspace(x_range[0], x_range[1], dims[0])
    y = np.linspace(y_range[0], y_range[1], dims[0])

    fig = {
        'data': [{
            'x': x,
            'y': y,
            'z': z,
            'type': 'heatmap',
            'showscale': False,
            'colorscale': [[0, 'rgba(255, 255, 255,0)'], [1, '#75baf2']],
            }],
        'layout': {
            'height': 350,
            'xaxis': {
                'title': 'Time [s]',
                'showline': True,
                'zeroline': False,
                'showgrid': False,
                'showticklabels': True
            },
            'yaxis': {
                'title': 'Amplitude',
                'fixedrange': True,
                'showline': False,
                'zeroline': False,
                'showgrid': False,
                'showticklabels': False,
                'ticks': ''
            },
            'title': 'Waveform'
        }
    }
    return fig
예제 #7
0
def percentile_clip(signal, p):
    """Normalize signal between 0 and 1, clipping peak values above given percentile

    Args:
        signal (th.tensor): Signal to normalize
        p (int): [0-100]. Percentile to clip to

    Returns:
        th.tensor: Clipped signal
    """
    locs = th.arange(0, signal.shape[0])
    peaks = th.ones(signal.shape, dtype=bool)
    main = signal.take(locs)

    plus = signal.take((locs + 1).clamp(0, signal.shape[0] - 1))
    minus = signal.take((locs - 1).clamp(0, signal.shape[0] - 1))
    peaks &= th.gt(main, plus)
    peaks &= th.gt(main, minus)

    signal = signal.clamp(0, percentile(signal[peaks], p))
    signal /= signal.max()
    return signal
예제 #8
0
def plot_signal_attr(fig, ax, attr, signal, fs=1.0, filter=True, lw=1.0):
    # Plots a signal with explanation strength as sample colors
    if filter:
        signal = bandpass_filter(signal, fs, fc=[1, 30])

    t = np.linspace(0, len(signal) / fs, len(signal))

    points = np.array([t, signal]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)

    # Create a continuous norm to map from data points to colors
    norm = plt.Normalize(attr.min(), attr.max())
    lc = LineCollection(segments, cmap='inferno_r', norm=norm)
    # Set the values used for colormapping
    lc.set_array(attr)
    lc.set_linewidth(lw)
    line = ax.add_collection(lc)

    ax.set_xlim(t.min(), t.max())
    ax.set_ylim(signal.min(), signal.max())
    ax.get_yaxis().set_ticks([])
    ax.get_xaxis().set_ticks([])
예제 #9
0
def vcorr(signal, corr, err=.01):
    if corr == 1:
        newsig = signal
    elif corr == -1:
        newsig = signal.max() - signal
    else:
        cval = 1. - (corr / 2. + .5)
        ncorr = 1
        sigfft = np.fft.rfft(signal)
        newsig = np.copy(signal)
        corrs = []
        # for x in range(100):
        while abs(ncorr - corr) > err:
            # randangs = 2*(np.random.rand(len(signal)) - .5)
            # randangs *= 2*np.pi*cval
            randangs = np.pi / 6. * np.random.randn(len(signal)) + cval * np.pi
            randangcnums = np.cos(randangs) + 1j * np.sin(randangs)
            newsigfft = sigfft * randangcnums[:len(signal) / 2 + 1]
            newsig = np.fft.irfft(newsigfft)
            ncorr = stats.pearsonr(newsig, signal)[0]
            corrs += [ncorr]
    # return corrs
    return newsig
예제 #10
0
    def _computeExB(self, data):
        """
        Giving the output of the conditional average
        it compute the amplitude of radial and poloidal
        electric field fluctuations taking into account the
        amplitude of the Isat conditional average structure
        and including only the fluctuation between 2.5 sigma of
        the Isat amplitude

        """

        signal = data.sel(sig='Is') - data.sel(sig='Is').min()
        spline = UnivariateSpline(data.t,
                                  signal.values - signal.max().item() / 2.,
                                  s=0)
        # find the roots and the closest roots to 0
        roots = spline.roots()
        tmin = roots[roots < 0][-1] * 2
        try:
            tmax = roots[roots > 0][0] * 2
        except:
            tmax = 2.5e-5
        # now the fluctuations of the Epol
        ii = np.where((data.t.values >= tmin) & (data.t.values <= tmax))[0]
        # recompute Epol from CAS floating potential with an appropriate
        # smoothing otherwise we have too noisy signal
        _Epol = (data.sel(sig='VFT_' + str(int(self.plunge))) -
                 data.sel(sig='VFM_' + str(int(self.plunge)))) / 4e-3
        _Epol = bottleneck.move_mean(_Epol, window=10)
        Epol = np.abs(_Epol[ii].max() - _Epol[ii].min())
        Erad = np.abs(
            data.sel(sig='Erad')[ii].max().item() -
            data.sel(sig='Erad')[ii].min().item())
        EpolErr = np.mean(data.err[1, ii])
        EradErr = np.mean(data.err[2, ii])
        out = {'Er': Erad, 'ErErr': EradErr, 'Epol': Epol, 'EpolErr': EpolErr}
        return out
예제 #11
0
    def compute(self):
        signal = self.getInputFromPort("Input").get_array().squeeze()
        print "signal.shape = ", signal.shape
        lof = self.getInputFromPort("Low Freq")
        hif = self.getInputFromPort("High Freq")

        num_scalar_bins = self.getInputFromPort("Scalar Bins")

        num_pts = signal.size
        min_s = signal.min()
        max_s = signal.max()
        d = max_s - min_s
        print "Accumulating over " + str(num_pts) + " points"
        histo = numpy.zeros((num_scalar_bins, hif - lof))

        f_sig = scipy.fftpack.fftn(signal)

        for z in range(signal.shape[0]):
            for y in range(signal.shape[1]):
                for x in range(signal.shape[2]):
                    yray = f_sig[z, :, x].squeeze()
                    ty = self.stockwell(yray, lof, hif)
                    zray = f_sig[:, y, x].squeeze()
                    tz = self.stockwell(zray, lof, hif)
                    xray = f_sig[z, y, :].squeeze()
                    tx = self.stockwell(xray, lof, hif)

                    tx = tx[:, z].squeeze()
                    ty = ty[:, y].squeeze()
                    tz = tz[:, x].squeeze()

                    tx = tx * tx.conjugate()
                    ty = ty * ty.conjugate()
                    tz = tz * tz.conjugate()

                    ar = tx.real + ty.real + tz.real
                    ar = ar / ar.sum()

                    scalar = signal[z, y, x]
                    try:
                        bin = int(
                            (scalar - min_s) / d * float(num_scalar_bins - 1.))
                        #                        dist[bin] += 1
                        #                        print bin, scalar, ar
                        sigma = self.forceGetInputFromPort("Sigma")
                        if sigma:
                            ar = scipy.signal.gaussian(ar.size, sigma) * ar
                        histo[bin, :] += ar
                    except:
                        print "Cannot assign to bin: " + str(
                            bin) + ", scalar: " + str(scalar)
                        print "location = ", x, y, z
                        raise ModuleError("Cannot assign to bin: " + str(bin) +
                                          ", scalar: " + str(scalar))


#                print "done with y = ", y
            print "done with z = ", z

        out = NDArray()
        out.set_array(histo)
        self.setResult("Output", out)
예제 #12
0
파일: demo.py 프로젝트: marcalph/slip
import numpy as np
import sounddevice as sd
import scipy.signal
url = "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3"
signal, resample_rate = librosa.load("file_example_MP3_700KB.mp3")
u = urlopen(url)

import miniaudio


import librosa
from src.features import plot_signal
import matplotlib.pyplot as plt
plot_signal(signal)
plt.show()
signal.max()

f = miniaudio.mp3_read_file_f32("file_example_MP3_700KB.mp3")
f.sample_rate
f.nchannels

f.samples

play(signal, 16000)
sd.stop()
len(signal)
873216/32000

pyaud = pyaudio.PyAudio()
srate=32000
stream = pyaud.open(format = pyaud.get_format_from_width(1),
예제 #13
0
def generate_feat_opts(path=None,
                       cfg={
                           'pkg': 'pysp',
                           'type': 'logfbank',
                           'nfilt': 40,
                           'delta': 2
                       },
                       signal=None,
                       rate=16000):
    cfg = dict(cfg)
    if cfg['pkg'] == 'pysp':  # python_speech_features #
        if signal is None:
            rate, signal = wavfile.read(path)

        if cfg['type'] == 'logfbank':
            feat_mat = pyspfeat.base.logfbank(signal,
                                              rate,
                                              nfilt=cfg.get('nfilt', 40))
        elif cfg['type'] == 'mfcc':
            feat_mat = pyspfeat.base.mfcc(signal,
                                          rate,
                                          numcep=cfg.get('nfilt', 26) // 2,
                                          nfilt=cfg.get('nfilt', 26))
        elif cfg['type'] == 'wav':
            feat_mat = pyspfeat.base.sigproc.framesig(
                signal,
                frame_len=cfg.get('frame_len', 400),
                frame_step=cfg.get('frame_step', 160))
        else:
            raise NotImplementedError(
                "feature type {} is not implemented/available".format(
                    cfg['type']))
            pass
        # delta #
        comb_feat_mat = [feat_mat]
        delta = cfg['delta']
        if delta > 0:
            delta_feat_mat = pyspfeat.base.delta(feat_mat, 2)
            comb_feat_mat.append(delta_feat_mat)
        if delta > 1:
            delta2_feat_mat = pyspfeat.base.delta(delta_feat_mat, 2)
            comb_feat_mat.append(delta2_feat_mat)
        if delta > 2:
            raise NotImplementedError(
                "max delta is 2, larger than 2 is not normal setting")
        return np.hstack(comb_feat_mat)
    elif cfg['pkg'] == 'rosa':
        if signal is None:
            signal, rate = librosa.core.load(path, sr=cfg['sample_rate'])

        assert rate == cfg[
            'sample_rate'], "sample rate is different with current data"

        if cfg.get('preemphasis', None) is not None:
            # signal = np.append(signal[0], signal[1:] - cfg['preemphasis']*signal[:-1])
            signal = signal_util.preemphasis(x, self.cfg['preemphasis'])

        if cfg.get('pre', None) == 'meanstd':
            signal = (signal - signal.mean()) / signal.std()
        elif cfg.get('pre', None) == 'norm':
            signal = (signal - signal.min()) / (signal.max() -
                                                signal.min()) * 2 - 1

        # raw feature
        if cfg['type'] == 'wav':
            if cfg.get('post', None) == 'mu':
                signal = linear2mu(signal)

            feat_mat = pyspfeat.base.sigproc.framesig(
                signal,
                frame_len=cfg.get('frame_len', 400),
                frame_step=cfg.get('frame_step', 160))
            return feat_mat
        # spectrogram-based feature
        raw_spec = signal_util.rosa_spectrogram(
            signal,
            n_fft=cfg['nfft'],
            hop_length=cfg.get('winstep', None),
            win_length=cfg.get('winlen', None))[0]
        if cfg['type'] in ['logmelfbank', 'melfbank']:
            mel_spec = signal_util.rosa_spec2mel(raw_spec, nfilt=cfg['nfilt'])
            if cfg['type'] == 'logmelfbank':
                return np.log(mel_spec)
            else:
                return mel_spec
        elif cfg['type'] == 'lograwfbank':
            return np.log(raw_spec)
        elif cfg['type'] == 'rawfbank':
            return raw_spec
        else:
            raise NotImplementedError()
    elif cfg['pkg'] == 'taco':
        # SPECIAL FOR TACOTRON #
        tacohelper = TacotronHelper(cfg)
        if signal is None:
            signal = tacohelper.load_wav(path)

        assert len(signal) != 0, ('file {} is empty'.format(path))

        try:
            if cfg['type'] == 'raw':
                feat = tacohelper.spectrogram(signal).T
            elif cfg['type'] == 'mel':
                feat = tacohelper.melspectrogram(signal).T
            else:
                raise NotImplementedError()
        except:
            import ipdb
            ipdb.set_trace()
            pass
        return feat
    elif cfg['pkg'] == 'world':
        if path is None:
            with tempfile.NamedTemporaryFile() as tmpfile:
                wavfile.write(tmpfile.name, rate, signal)
                logf0, bap, mgc = world_vocoder_util.world_analysis(
                    tmpfile.name, cfg['mcep'])
        else:
            logf0, bap, mgc = world_vocoder_util.world_analysis(
                path, cfg['mcep'])

        vuv, f0, bap, mgc = world_vocoder_util.world2feat(logf0, bap, mgc)

        # ignore delta, avoid curse of dimensionality #
        return vuv, f0, bap, mgc
    else:
        raise NotImplementedError()
        pass