0
$\begingroup$

The idea here is to improve a Sinc function by manipulating its Fourier transform.

As you can see, the tophat has ripples in the bands of interest and I want to eliminate them by smoothing them with a window or some filtering method, then take the DFT of the smoothed tophat to get the Sinc.

I want to compare the Sinc obtained from the smoothed tophat with the actual Sinc used to get the rippled tophat.

import numpy as np
import matplotlib.pyplot as plt
def fft(p):
  return np.fft.fftshift(np.fft.fft(np.fft.fftshift(p)))

##define discrete Fourier transform
def DFT(x):
    N = x.size
    #Create vector to store result in
    X = np.zeros(N, dtype=complex)
    for k in range(N):
        for n in range(N):
            X[k] += np.exp(-1j * 2.0* np.pi* k * n / N) * x[n]
    return X
x = np.array([-2.81131875e-02, -3.96997713e-02, -4.21368144e-02, -3.44452846e-02,
 -1.80464821e-02,  3.47617152e-03  ,2.51429695e-02,  4.17113849e-02,
  4.89232237e-02,  4.45735920e-02,  2.91392108e-02,  5.79606030e-03
, -2.02066174e-02, -4.26991128e-02, -5.60228061e-02, -5.64157843e-02,
 -4.30215932e-02, -1.82621467e-02 , 1.25480044e-02,  4.22981416e-02,
  6.36705197e-02,  7.08779634e-02,  6.11503378e-02,  3.56057506e-02,
 -7.36946338e-04, -3.98641156e-02, -7.24435298e-02, -8.99361403e-02,
 -8.66871186e-02, -6.15139083e-02, -1.83898004e-02,  3.40030403e-02,
  8.38410834e-02,  1.18587835e-01,  1.27818064e-01,  1.05851694e-01,
  5.36133160e-02, -2.07428262e-02, -1.02672849e-01, -1.73486957e-01,
 -2.13645119e-01, -2.06513036e-01, -1.41864949e-01, -1.84298219e-02,
  1.55066260e-01,  3.60639086e-01,  5.73509978e-01,  7.65904176e-01,
  9.11528887e-01,  9.89928563e-01,  9.89928563e-01,  9.11530481e-01,
  7.65911712e-01,  5.73528030e-01,  3.60669975e-01,  1.55108326e-01,
 -1.83838723e-02, -1.41826535e-01, -2.06494808e-01, -2.13657425e-01,
 -1.73533710e-01, -1.02749119e-01, -2.08345369e-02 , 5.35272126e-02,
  1.05793985e-01,  1.27807692e-01 , 1.18633975e-01,  8.39393878e-02,
  3.41351502e-02, -1.82532023e-02, -6.14064529e-02, -8.66385973e-02,
 -8.99643861e-02, -7.25486656e-02, -4.00269498e-02, -9.22081286e-04,
  3.54422496e-02,  6.10504637e-02,  7.08711442e-02,  6.37651928e-02,
  4.24781826e-02,  1.27749970e-02, -1.80408041e-02 ,-4.28604510e-02,
 -5.63580073e-02, -5.60886911e-02, -4.28797388e-02, -2.04643427e-02
 , 5.51990784e-03,  2.89109493e-02,  4.44513443e-02,  4.89421251e-02,
  4.18738004e-02,  2.54163255e-02,  3.79921602e-03, -1.77499417e-02,
 -3.42484465e-02 ,-4.20918559e-02, -3.98241253e-02, -2.83838352e-02])
def sinc(x):
        xu =  np.sin(x)/x
        xu[x==0] = 1
        return xu
xx = sinc(x)
dft = DFT(xx)
plt.plot(xx)
plt.figure()
plt.plot(dft/np.max(dft),label='DFT')
plt.plot(fft(xx)/np.max(fft(xx)),label='FFT')

where xx is some 1D Sinc function.

enter image description here

$\endgroup$
22
  • 1
    $\begingroup$ Welcome to SE.SP! Please include all code that is required to run. This code is missing import statements, and many of the variables and functions are redefined or referred to differently in different places (e.g. sinc vs sinc0 or x vs x0. Ping me once you've edited your question with these additions and I'll reopen it. $\endgroup$
    – Peter K.
    Sep 1, 2022 at 13:35
  • $\begingroup$ @PeterK. I have edited it. $\endgroup$
    – visco
    Sep 1, 2022 at 14:56
  • 1
    $\begingroup$ @PeterK, is it common to allow questions that are simply code dumps? $\endgroup$
    – Gillespie
    Sep 1, 2022 at 20:22
  • 1
    $\begingroup$ This questions would really benefit from a) describe what you are trying to do, b) describe how you current results are different from what you expect, c) put some comments in your code to describe what you are trying to do. $\endgroup$
    – Hilmar
    Sep 1, 2022 at 20:51
  • 1
    $\begingroup$ @Gillespie Ya. Probably not. $\endgroup$
    – Peter K.
    Sep 1, 2022 at 21:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.