示例#1
0
def noise(nblock=None, state=None, color='white', ntaps=None):
    """Generate white noise with standard Gaussian distribution.

    :param nblock: Amount of samples per block.
    :param state: State of PRNG.
    :type state: :class:`np.random.RandomState`
    :returns: When `nblock=None`, individual samples are generated and a :class:`streaming.Stream` is
    returned. When integer, a :class:`streaming.BlockStream` is returned.
    """
    if state is None:
        state = np.random.RandomState()

    # Generate white noise
    if nblock is None:
        # Generate individual samples.
        stream = Stream((state.randn(1)[0] for i in itertools.count()))
    else:
        # Generate blocks.
        stream = BlockStream((state.randn(nblock) for i in itertools.count()),
                             nblock=nblock,
                             noverlap=0)

    # Apply filter for specific color
    if color is not 'white':
        if ntaps is None:
            raise ValueError("Amount of taps has not been specified.")
        ir = noisy.COLORS[color](ntaps)
        if nblock is None:
            nhop = ntaps
        else:
            nhop = max(nblock, ntaps)
        stream = convolve_overlap_add(stream, constant(ir), nhop,
                                      ntaps).samples().drop(ntaps // 2)

    # Output as desired
    if nblock is None:
        return stream.samples()
    else:
        return stream.blocks(nblock)
示例#2
0
文件: signal.py 项目: FRidh/streaming
def noise(nblock=None, state=None, color='white', ntaps=None):
    """Generate white noise with standard Gaussian distribution.

    :param nblock: Amount of samples per block.
    :param state: State of PRNG.
    :type state: :class:`np.random.RandomState`
    :returns: When `nblock=None`, individual samples are generated and a :class:`streaming.Stream` is
    returned. When integer, a :class:`streaming.BlockStream` is returned.
    """
    if state is None:
        state = np.random.RandomState()

    # Generate white noise
    if nblock is None:
        # Generate individual samples.
        stream = Stream((state.randn(1)[0] for i in itertools.count()))
    else:
        # Generate blocks.
        stream = BlockStream((state.randn(nblock) for i in itertools.count()), nblock=nblock, noverlap=0)

    # Apply filter for specific color
    if color is not 'white':
        if ntaps is None:
            raise ValueError("Amount of taps has not been specified.")
        ir = noisy.COLORS[color](ntaps)
        if nblock is None:
            nhop = ntaps
        else:
            nhop = max(nblock, ntaps)
        stream = convolve_overlap_add(stream, constant(ir), nhop, ntaps).samples().drop(ntaps//2)

    # Output as desired
    if nblock is None:
        return stream.samples()
    else:
        return stream.blocks(nblock)
示例#3
0
def stream(sequence, blocked, nblock):
    stream = Stream(sequence)
    if blocked:
        stream = stream.blocks(nblock)
    return stream