<jupyter_start><jupyter_text>Two-Dimensional Fourier Transforms*This Jupyter notebook is part of a [collection of notebooks](../index.ipynb) in the masters course Selected Topics in Audio Signal Processing, Communications Engineering, Universität Rostock. Please direct questions and suggestions to [<EMAIL>](mailto:<EMAIL>).* First a function is defined which computes the two-dimensional Fourier transform of a two-dimensional signal and plots the resulting spectrum.<jupyter_code>%matplotlib inline
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt


def Fourier_transform(f):
    # computes and plots the magnitude spectrum
    
    F = np.fft.fft2(f)
    F = np.fft.fftshift(F)
    F = F / np.max(np.abs(F))
    magnitude_spectrum = 20*np.log(np.abs(F))

    plt.figure(figsize=(6,6))
    plt.imshow(np.real(f), cmap = 'gray')
    plt.title('$p(x, y)$')
    plt.xlabel('$x$')
    plt.ylabel('$y$')

    plt.figure(figsize=(7,6))
    plt.imshow(magnitude_spectrum, cmap = 'gray', extent=[-f.shape[1]/2, f.shape[1]/2, -f.shape[0]/2, f.shape[0]/2])
    plt.title(r'$|\tilde{p}(k_x, k_y)|$ in dB')
    plt.xlabel('$k_x$')
    plt.ylabel('$k_y$')
    plt.colorbar(fraction=0.042, pad=0.04)<jupyter_output><empty_output><jupyter_text>Single Plane Wave The two-dimensional Fourier transform of a two-dimensional plane wave is considered. First the sound field is computed, followed by the computation of its Fourier transform. Both the sound field as well as its Fourier transform are plotted.<jupyter_code>kx = 1
ky = 5

X,Y = np.meshgrid(np.linspace(-10, 10, num=200), np.linspace(-10, 10, num=200))
p = np.exp(-1j*(kx*X + ky*Y))

Fourier_transform(p)<jupyter_output><empty_output><jupyter_text>Multiple Plane WavesNow a superposition of multiple plane waves is considered<jupyter_code>p = np.zeros(X.shape)
kx = [-5, -15, 0, 20, 30]
ky = [4, -8, 12, -5, 3]

for n in range(5):
    X,Y = np.meshgrid(np.linspace(-10, 10, num=200), np.linspace(-10, 10, num=200))
    p = p + np.exp(-1j*(kx[n]*X + ky[n]*Y))

Fourier_transform(p)<jupyter_output><empty_output><jupyter_text>Natural ImageThe last example shows the two-dimensional Fourier transform of a grayscale image.<jupyter_code>def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

f = rgb2gray(misc.face())
Fourier_transform(f)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1469838572642311, 'fraction_numerical': 0.0356839422259983, 'mean_word_length': 4.7299270072992705, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '352098', 'license': 'MIT', 'n_tokens_mistral': 939, 'n_tokens_neox': 859, 'n_words': 220, 'path': 'sound_field_analysis/2d_Fourier_transform.ipynb', 'repo_name': 'spatialaudio/-selected-topics-in-audio-signal-processing-lecture-', 'size': '2394'}