<jupyter_start><jupyter_text>AG Dynamics of the Earth    Juypter notebooks    <NAME>      Angewandte Geophysik II: Kap 9: Filterung 2D FFT with simple squares example----*<NAME>,Geophysics Section,Institute of Geological Sciences,Freie Universität Berlin,Germany*Generate a number of squares with value $1$ in a background with value $0$, thenFourier-transform image, apply a Gaussian blur filter, and the inverse Fourier transform.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
colors = [(1, 1, 1), (0, 0, 0)]  # R -> G -> B
cmap_name = 'my_list'
cm = LinearSegmentedColormap.from_list(cmap_name, colors, N=5)
tiny=1e-15
# image size, square side length, number of squares
nx,ny    = 120, 120
sq_size  = 10
nsquares = 10

# The image array (0=background, 1=square) and boolean array of allowed places
# to add a square so that it doesn't touch another or the image sides
image = np.zeros((nx, ny))
sq_locs = np.zeros((nx, ny), dtype=bool)
sq_locs[1:-sq_size-1:,1:-sq_size-1] = True

def place_square():
    """ Place a square at random on the image and update sq_locs. """
    # valid_locs is an array of the indices of True entries in sq_locs
    valid_locs = np.transpose(np.nonzero(sq_locs))
    # pick one such entry at random, and add the square so its top left
    # corner is there; then update sq_locs
    i, j = valid_locs[np.random.randint(len(valid_locs))]
    image[i:i+sq_size, j:j+sq_size] = 1
    imin, jmin = max(0,i-sq_size-1), max(0, j-sq_size-1)
    sq_locs[imin:i+sq_size+1, jmin:j+sq_size+1] = False

# Add the required number of squares to the image
for i in range(nsquares):
    place_square()
    
# Take the 2-dimensional DFT and centre the frequencies
ftimage = np.fft.fft2(image)
ftimage = np.fft.fftshift(ftimage)

# Build and apply a Gaussian filter.
def gmask(sigmax=30,sigmay=30):
    cy, cx = nx/2, ny/2
    x = np.linspace(0, nx, nx)
    y = np.linspace(0, ny, ny)
    X, Y = np.meshgrid(x, y)
    gmask = np.exp(-(((X-cx)/sigmax)**2 + ((Y-cy)/sigmay)**2))
    return gmask


# Finally, take the inverse transform and show the blurred image
ftimagep = ftimage * gmask(8,8)
imagep = np.fft.ifft2(ftimagep)

power  = np.abs(ftimage)**2
power  = 20*np.log10((power+tiny)/power.max())
powerp = np.abs(ftimagep)**2
powerp = 20*np.log10((powerp+tiny)/powerp.max())
fig,axs = plt.subplots(2,2,figsize=(12.0,8.0))

axs[0,0].set_title('random squares')
axs[0,0].imshow(image,cmap=cm)
axs[0,1].set_title('spectrum')
axs[0,1].imshow(power,cmap=cm)
axs[1,0].set_title('filtered random squares')
axs[1,0].imshow(np.abs(imagep),cmap=cm)
axs[1,1].set_title('spectrum')
axs[1,1].imshow(powerp,cmap=cm)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13708195516354282, 'fraction_numerical': 0.03123851525174568, 'mean_word_length': 4.600823045267489, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '132851', 'license': 'MIT', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1041, 'n_words': 280, 'path': '.ipynb_checkpoints/AGII_chap09_FFT2_smooth_squares-checkpoint.ipynb', 'repo_name': 'georgkaufmann/lecture_agII', 'size': '2737'}