<jupyter_start><jupyter_text>Part 1:In a Jupyter python notebook, create a 256x256 pixel numpy array with a single pixel with value 1 roughly in the center.<jupyter_code>img_base = np.zeros((256,256))
x0 = 150
y0 = 110
img_base[y0, x0] = 1<jupyter_output><empty_output><jupyter_text>Question 1a: Shifting images in image spaceUse scipy.ndimage.shift to shift a copy of the image by a fraction of a pixel (say 0.34 in x and 0.2 in y). FFT the shifted image and the original, and divide the FFT of the shifted image by the FFT of the original. Make two images of the result: one of the amplitude, the other of the phase.<jupyter_code>scipy_shift?
dx = .2
dy = .34
img_shift = scipy_shift(img_base, [dy, dx], prefilter=True, order=1)
plot_image(img_base[y0-10:y0+10,x0-10:x0+10],"Original Image")
plot_image(img_shift[y0-10:y0+10,x0-10:x0+10],"Shifted Image")<jupyter_output><empty_output><jupyter_text>Fourier space<jupyter_code>img0_fft = np.fft.fftshift(np.fft.ifft2(img_base))
img1_fft = np.fft.fftshift(np.fft.ifft2(img_shift))
fft_ratio = img1_fft/img0_fft
plot_image(np.abs(fft_ratio),"Abs(ratio)")
plot_image(np.real(fft_ratio),"real(ratio)")
plot_image(np.angle(fft_ratio),"angle(ratio)")<jupyter_output><empty_output><jupyter_text>Now calculate the shift in Fourier space<jupyter_code>def calculate_phase_shift(dimension, elements, dx, dy):
    icomp = complex(0,1)
    xv, yv = np.meshgrid(np.arange(dimension), np.arange(elements))
    phase = np.exp(2*icomp*np.pi*((xv - dimension/2)*dx/dimension + (yv - elements/2)*dy/elements))
    return phase

phase = calculate_phase_shift(256,256, dx, dy)
plot_image(np.angle(phase),"UV phase")
img0_fft_uv_shift = img0_fft*phase
img_uv_shifted = np.real(np.fft.fft2(np.fft.fftshift(img0_fft_uv_shift)))
plot_image(img_uv_shifted[y0-10:y0+10,x0-10:x0+10],"UV shifted Image")
plot_image(img_shift[y0-10:y0+10,x0-10:x0+10],"Shifted Image")<jupyter_output><empty_output><jupyter_text>Question 2a: AliasingCreate a 256x256 pixel numpy array containing a 2D gaussian at the center, with a width roughly 1/10 the size of the full image (it should look compact). Take the FFT of a copy, and make an image of the real part of the result. If it oscillates, you need to apply an FFT shift first. Use this image as a reference to create the analytic Fourier transform of your gaussian directly in Fourier space. FFT the new Fourier analytic gaussian back to image space, and subtract the result from the original. Make an image of the residual.<jupyter_code>def gaussian2d (dimension, elements, xcenter = 128, ycenter = 128, sigmax = 25.6, sigmay = 25.6, use_fourier=False):
    xv, yv = np.meshgrid(np.arange(dimension), np.arange(elements))
    diffx = xv - xcenter
    diffy = yv - ycenter

    a_x = 1/(2*sigmax**2)
    a_y = 1/(2*sigmay**2)
    k_x = 2*np.pi
    k_y = 2*np.pi
    if use_fourier:
        pre_x = k_x*np.pi**2/(a_x*2*np.pi*dimension**2)
        pre_y = k_y*np.pi**2/(a_y*2*np.pi*dimension**2)
    else:
        pre_x = a_x
        pre_y = a_y
    print(pre_x)
    gaussian = np.exp(-((pre_x*diffx**2)+(pre_y*diffy**2)))
    return gaussian

img_gaussian0 = gaussian2d(256,256)
plot_image(img_gaussian0,"Gaussian Image")
img_gaussian0_fft = np.sqrt(256)*np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(img_gaussian0)))
plot_image(np.real(img_gaussian0_fft[128-10:128+10,128-10:128+10]),"FFT of Gaussian Image")
img_gaussian1_fft = gaussian2d(256,256, use_fourier=True)
plot_image(img_gaussian1_fft[128-10:128+10,128-10:128+10],"Gaussian Image")
cutout1 = img_gaussian1_fft[128-10:128+10,128-10:128+10]
cutout2 = np.real(img_gaussian0_fft[128-10:128+10,128-10:128+10])
cutout1 *= np.max(cutout2)/np.max(cutout1)
plot_image(cutout1 - cutout2,"Difference of the FFT Gaussian")<jupyter_output><empty_output><jupyter_text><jupyter_code>2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1291251964379256, 'fraction_numerical': 0.0678365636458879, 'mean_word_length': 5.747349823321555, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '440219', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1525, 'n_words': 328, 'path': 'ASTR 499 Fall 2021 assignment 1.ipynb', 'repo_name': 'isullivan/astr499', 'size': '3818'}