how can I have a perfect rect function as the result of applying fft on sinc function?

I have to generate a perfect rectangle as the fft result of the sinc function. I am working with different parameters to achieve this, so far I could generate a good rectangle but it still has some imperfections. I want exactly a rectangle like what we have in theory. I attached my code and results to this message. Two problems that I have in this rect function:
1- spikes in the edges
2- gliteches along with the rect function
clear all;
Fs=2000; % sampling frequency
Ts=1/Fs; % sampling period
t=-1:Ts:1;
N=numel(t); % numel recommended instead of length
f=500;
cx=sinc(t*f);
cx(end-300:end)=0; cx(1:300)=0;
figure;
plot(t,cx);title("sinc function");
xlabel('time');
ylabel(' magnitude');
fy=(fft(cx));
Nyq = Fs/2; % Nyquist frequency is 1/2 of the sampling frequency
% dx = (t(end)-t(1))/(N-1); % Time increment, should rename to dt, but not used
% suggest not using colon with df stride, stick colon with unit stride
if mod(N,2) == 0 % N is even
k = ( (-N/2) : ((N-2)/2) )/N*Fs;
else % N is odd
k = ( (-(N-1)/2) : ((N-1)/2) )/N*Fs;
end
figure;
plot(k,fftshift(abs(fy*Ts)));title("rect function");
xlabel(' frequency t^{-1}');
ylabel(' magnitude');
xlim([-2000 2000])

Réponses (1)

Matt J
Matt J le 27 Juin 2022
Modifié(e) : Matt J le 28 Juin 2022
I want exactly a rectangle like what we have in theory.
The FFT of a sinc function is not a perfect rect, even in theory. The continuous Fourier Transform of a continuous and infinitely long sinc function is a perfect rect.
The only way you can get a perfect discretized rect as the output of the fft is to start with the ifft of a perfect rect.

4 commentaires

To this answer, I would add that one period of the discrete time Fourier transform (DTFT) of a discrete and infinitely long sinc function is a perfect rect, but the DTFT of an infinitley long function cannot be computed numerically (at least I don't thing it can).
I thought it might be interesting to explore this statement a bit:
The only way you can get a perfect discretized rect as the output of the fft is to start with the ifft of a perfect rect.
One period of the DTFT of a*sinc(a*n) is a rect function with edges at +- a*pi.
Let's construct the DFT by sampling the rect function and assuming the samples at the edges are unity
n = -100:100;
N = numel(n);
X1 = 0*n;
an = 30;
X1(n>= -an & n <= an) = 1;
With these parameters, we have
a = 2*an/N; % 2*pi*an/N = pi*a;
The ifft of X1 is
x1 = fftshift(ifft(ifftshift(X1)));
The imaginary part of x1 is zero (no rounding error?)
max(abs(imag(x1)))
ans = 0
x1 looks a lot like a rectangular window of width N applied to a*sinc(a*n), but there are noticeable gaps, particularly toward the edges. Also, keep in mind that x1[n] = 0 for abs(n) > 100
figure
plot(n,x1,n,a*sinc(a*n))
Plot the difference between them
figure
plot(n,x1-a*sinc(a*n))
So not quite the window of a*sinc(a*n).
However, sampling the edges of the rect DTFT is a tricky business. It may be more appropirate to assign those values as 1/2.
X2 = X1;
X2(find(X2==1,1,'first')) = 1/2;
X2(find(X2==1,1,'last')) = 1/2;
x2 = fftshift(ifft(ifftshift(X2)));
max(abs(imag(x2)))
ans = 0
Now compare x2 and x1 to the the sinc
figure;
plot(n,x1-a*sinc(a*n),n,x2-a*sinc(a*n))
So x2 is a closer match to the windowed sinc, notably at lower times, but still not a sinc.
Of course, as @Matt J pointed out, we should never expect to recover a sinc from an IDFT because the IDFT applies to finite duration signals and sinc is infnite duration.
Thank you, Paul, it was a thoughtful solution. I searched these few days to see how I can even make that rect function close to a perfect rect, and also not by starting from a rect function and its ifft. I increased the period and somehow the result seems good, but I should use advanced ring removal techniques to make it better.
To be clear, I wasn't offerering a solution, If anything, I was illustrating that a solution doesn't exist.
In summary:
CT sinc -> CTFT -> rect
DT sinc -> DTFT -> periodic extension of rect
DT sinc -> DFT -> can't be done because DFT only applies for finite duration signals, and sinc is infinite duration
DF (discrete frequency) rect -> IDFT -> something close sinc*rect

Connectez-vous pour commenter.

Produits

Version

R2021a

Question posée :

le 27 Juin 2022

Commenté :

le 1 Juil 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by