Example #1
0
 def _hilbert(self, data):
     pci1 = fft.fft2(fft.fftshift(np.float32(data)))
     pci2 = fft.ifftshift(pci1) * self.filter1
     fpci0 = fft.ifftshift(fft.ifft2(fft.fftshift(pci2)))
     fpci = np.imag(fpci0)
     result = fpci
     return result
 def _hilbert(self, data):
     pci1 = fft.fft2(fft.fftshift(np.float32(data)))
     pci2 = fft.ifftshift(pci1)*self.filter1
     fpci0 = fft.ifftshift(fft.ifft2(fft.fftshift(pci2)))
     fpci = np.imag(fpci0)
     result = fpci
     return result
Example #3
0
def simcheck(data, nphases):
    norients = data.shape[0]//nphases       # need to use integer divide

    # check user input before proceeding
    assert nphases*norients == data.shape[0]

    newshape = norients, nphases, data.shape[-2], data.shape[-1]

    # FT data only along spatial dimensions
    ft_data = fftshift(
                fftn(ifftshift(
                    data, axes=(1, 2)),
                 axes=(1, 2)),
            axes=(1, 2))
    # average only along phase, **not** orientation
    # This should be the equivalent of the FT of the average image per each
    # phase (i.e it should be symmetric as the phases will have averaged out)
    ft_data_avg = ft_data.reshape(newshape).mean(1)
    # Do the same, but take the absolute value before averaging, in this case
    # the signal should add up because the phase has been removed
    ft_data_avg_abs = np.abs(ft_data).reshape(newshape).mean(1)
    # Take the difference of the average power and the power of the average
    ft_data_diff = ft_data_avg_abs-abs(ft_data_avg)

    return ft_data_diff, ft_data_avg_abs, ft_data_avg
Example #4
0
 def psf_correction(self, mat, win, pad_width):
     (nrow, ncol) = mat.shape
     mat_pad = np.pad(mat, pad_width, mode="reflect")
     win_pad = np.pad(win, pad_width, mode="constant", constant_values=1.0)
     mat_dec = fft.ifft2(fft.fft2(mat_pad) / fft.ifftshift(win_pad))
     return np.abs(mat_dec)[pad_width:pad_width + nrow,
                            pad_width:pad_width + ncol]
Example #5
0
    def process_frames(self, data):
        sino = data[0]
        sino2 = np.fliplr(sino[1:])
        (Nrow, Ncol) = sino.shape
        mask = self._create_mask(
            2*Nrow-1, Ncol, 0.5*self.parameters['ratio']*Ncol)

        FT1 = fft.fftshift(fft.fft2(np.vstack((sino, sino2))))
        sino = fft.ifft2(fft.ifftshift(FT1 - FT1*mask))
        return sino[0:Nrow].real
Example #6
0
def sampling_op_adjoint(image, mask_as_image):
    """ Adjoint of sampling operator
    
    :param image: Assumed to be an array with shape [N,N]
    :param mask_as_image: 
    :param mask_as_image: Mask of shape [N,N] where middle of the image 
                          corresponds to zero frequency. Values should be 0,1 or
                          True/False.
    :return: Array of shape [N, N] with the adjoint data.
    """
    N = max(image.shape)
    image_domain_data = fftw.ifft2(fftw.ifftshift(image)) * N
    return image_domain_data
def fir_window_bp(delta, fl, fh):
    """
    Finite impulse response, bandpass.
    This filter doesn't work exactly like the matlab version due to some fourier transform imprecisions.
    Consider replacing the transform calls to the FFTW versions.
    """
    b = firwin(delta.shape[0]+1, (fl*2, fh*2), pass_zero=False)[:-1]
    m = delta.shape[1]
    batches = 20
    batch_size = int(m / batches) + 1
    temp = fft(ifftshift(b))
    out = zeros(delta.shape, dtype=delta.dtype)
    for i in range(batches):
        indexes = (batch_size*i, min((batch_size*(i+1), m)))
        freq = fft(delta[:,indexes[0]:indexes[1]], axis=0)*tile(temp, (delta.shape[2],indexes[1]-indexes[0], 1)).swapaxes(0,2)
        out[:, indexes[0]:indexes[1]] = real(ifft(freq, axis=0))
    return out
Example #8
0
def spectral_whitening(tr,
                       smooth=None,
                       filter=None,
                       waterlevel=1e-8,
                       mask_again=True):
    """
    Apply spectral whitening to data

    Data is divided by its smoothed (Default: None) amplitude spectrum.

    :param tr: trace to manipulate
    :param smooth: length of smoothing window in Hz
        (default None -> no smoothing)
    :param filter: filter spectrum with bandpass after whitening
        (tuple with min and max frequency)
    :param waterlevel: waterlevel relative to mean of spectrum
    :param mask_again: weather to mask array after this operation again and
        set the corresponding data to 0

    :return: whitened data
    """
    sr = tr.stats.sampling_rate
    data = tr.data
    data = _fill_array(data, fill_value=0)
    mask = np.ma.getmask(data)
    nfft = next_fast_len(len(data))
    spec = fft(data, nfft)
    spec_ampl = np.abs(spec)
    spec_ampl /= np.max(spec_ampl)
    if smooth:
        smooth = int(smooth * nfft / sr)
        spec_ampl = ifftshift(smooth_func(fftshift(spec_ampl), smooth))
    # save guard against division by 0
    spec_ampl[spec_ampl < waterlevel] = waterlevel
    spec /= spec_ampl
    if filter is not None:
        spec *= _filter_resp(*filter, sr=sr, N=len(spec), whole=True)[1]
    ret = np.real(ifft(spec, nfft)[:len(data)])
    if mask_again:
        ret = _fill_array(ret, mask=mask, fill_value=0)
    tr.data = ret
    return tr
Example #9
0
 def apply_filter(self, mat, window, pattern, pad_width):
     (nrow, ncol) = mat.shape
     if pattern == "PROJECTION":
         top_drop = 10  # To remove the time stamp at some data
         mat_pad = np.pad(mat[top_drop:],
                          ((pad_width + top_drop, pad_width),
                           (pad_width, pad_width)),
                          mode="edge")
         win_pad = np.pad(window, pad_width, mode="edge")
         mat_dec = fft.ifft2(
             fft.fft2(-np.log(mat_pad)) / fft.ifftshift(win_pad))
         mat_dec = np.abs(mat_dec[pad_width:pad_width + nrow,
                                  pad_width:pad_width + ncol])
     else:
         mat_pad = np.pad(-np.log(mat), ((0, 0), (pad_width, pad_width)),
                          mode='edge')
         win_pad = np.pad(window, ((0, 0), (pad_width, pad_width)),
                          mode="edge")
         mat_fft = np.fft.fftshift(fft.fft(mat_pad), axes=1) / win_pad
         mat_dec = fft.ifft(np.fft.ifftshift(mat_fft, axes=1))
         mat_dec = np.abs(mat_dec[:, pad_width:pad_width + ncol])
     return np.float32(np.exp(-mat_dec))
Example #10
0
def cifftn(data,axes):
    '''Centered ifft'''
    return fftpack.fftshift(fftpack.ifftn(fftpack.ifftshift(data,axes),axes=axes))
Example #11
0
 def ifftshift(self, a, axes=None):
     return scipy_fft.ifftshift(a, axes=axes)