Example #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
Example #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)
Example #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)
Example #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)
Example #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
Example #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
Example #7
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([])
Example #8
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)
Example #9
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