def downsample(signal, fs, fmax):
    """Downsamples signal by integer factor if fs > 2 * fmax

    Downsamples the signal output if downsampling is possible.
    Downsampling is possible when fs > 2*fmax, where 2*fmax is the Nyquist
    frequency for highest frequency of interest.

    Parameters
    ----------
    signal : generator
        Generator that yields a 1D np.array containing data samples
    fs : int
        Sampling rate of the unmodified signal
    fmax : int
        The highest frequency of interest.
        Frequencies higher than fmax may be removed during downsampling

    Returns
    -------
    ds_signal : generator
        Generator that yields chunks (np.array) containing data samples.
        Data samples are downsampled if downsampling is possible, otherwise
        the original signal generator is returned
    ds_fs : int
        The downsampled sampling rate. If downsampling is not possible
        then the original sampling rate is returned.
    """
    if fs < 2 * fmax:
        raise ValueError('Sampling frequency fs must be at least 2 * fmax')
    n = int(fs / (2 * fmax))
    if n == 1:
        # Downsampling is not possible
        return signal, fs
    else:
        # Downsample the signal generator
        ds_signal = (chunk[::n] for chunk in signal)
        ds_fs = int(fs // n)
        return ds_signal, ds_fs