# Scaling of FFT2 magnitude in image-processing

I got the following code:

import numpy as np
from skimage import data

def plot_spectrum(fft_im, vmin, scale_factor):
fshift0 = np.fft.fftshift(fft_im) #shifts the zero-frequency component to the center of the spectrum
# (log10(|(fft*(10^6)-1) / ((10^5)+1 ) / (10^5)|) + 5 ) )* 1 / (scale_factor+1)
magnitude_spectrum = (np.log10(np.abs(fshift0*(np.power(10,scale_factor+1)-1)/np.power(10,scale_factor)+1/np.power(10,scale_factor)))+scale_factor)*1/(scale_factor+1)

plt.imshow(np.abs(magnitude_spectrum),  vmin=vmin)

imp = data.camera()
fft_im = np.fft.fft2(fft_im)
im_res = plot_spectrum(fft_im, vmin=0, scale_factor=5)


I don't understand the following line:

magnitude_spectrum = (np.log10(np.abs(fshift0*(np.power(10,scale_factor+1)-1)/np.power(10,scale_factor)+1/np.power(10,scale_factor)))+scale_fact


What is it doing and from what is it derived?

This is the important part: np.log10(np.abs(fshift0)). It does a logarithmic scaling of the magnitude of the complex spectrum. It is common to display the Fourier transform this way because of the large dynamic range.

The rest is scaling the input to the log function, so you get a nice view of the data, and scaling its output, probably to get it in the 0-1 range expected by some display functions.

The next line displays np.abs(magnitude_spectrum), which makes me think the output scaling is not completely correct. Or maybe the abs is accidentally left in there? Anyway, you shouldn’t take the magnitude twice.

• I updated the code. Thanks for the abs hint. I replaced the line by magnitude_spectrum = 20*np.log2( np.abs(ad_fft) ) and removed the abs in the imshow. More or less same output. Commented Jan 9, 2022 at 19:33