2

I have some problems with transforming my data to the f-k domain. I could see many examples on this site about DFT using Matlab. But each of them has little difference. Their process is almost the same, but there is a difference in the DFT algorithm. what I saw is

%Setup domain
s = size(data);

%time domain
nt = s(1); %number of time sample
dt = time(2); %time interval

%position domain
nx = s(2); %number of position sample
dx = position(2); %position interval

%Setup wavenumber & frequency
%frequency
Nyq_f = 1/(2*dt);
df = 1/(nt*dt);   % Frequency increment
f = -Nyq_f : df : Nyq_f-df; % frequency

%wavenumber
Nyq_k = 1/(2*dx); % Nyquist of data in first dimension
dk = 1/(nx*dx);   % Wavenumber increment
k = -Nyq_k : dk : Nyq_k-dk; % wavenumber

for j1 = 1 : Nt
    for j2 = 1 : Nt
        data_frequency(j1) = data_frequency(j1) + ...
                    data_time(j2)*exp(-1i*(2*pi)*(f(j1)*t(j2)))*dt;
    end
end

In that code, f means frequency, t means time, dt means time interval. But many of other codes, they use this code:

for k=1:nfft
    for n=1:nfft
        X(k) = X(k) + f(n)*exp(-j*2*pi*(k-1)*(n-1)/N);
    end
end

I couldn't understand why the results of the first code and the result of fft function are the same without dividing by the data length.

And the second question is, my data's matrix size is not the power of 2. In this case, I know that when I use the FFT function, I need to use zero-padding to the data for the matrix size. So, should I use zero-padding for the data even when using the DFT code?

  • 1
    It's not strictly necessary to zero-pad to a power of 2 for the `fft` function, but doing so could make the code run faster. – aschepler Jan 24 '21 at 14:28
  • 1
    What that first but of code does depends on how `f` and `t` are defined. – Cris Luengo Jan 24 '21 at 15:31
  • @aschepler I tried to compare the `fft` function using the zero-padding and not using. But each result is different. Do I don't need to use the zero-padding? – Hak-Min Lee Jan 24 '21 at 15:52
  • @CrisLuengo For the writing the code, I make the new question below. – Hak-Min Lee Jan 24 '21 at 16:04
  • 1
    Below is for answering the question in the post. To reply to comments, please [edit] your question rather than adding an answer. – Cris Luengo Jan 24 '21 at 16:06
  • 1
    Anyway, if you replace the values of `f` and `t` into the code you will probably see that the two bits of code do the same thing. – Cris Luengo Jan 24 '21 at 16:08
  • @CrisLuengo I edited the question. Those defined `f` and `t` do the same thing with `/N` in the second code? – Hak-Min Lee Jan 24 '21 at 16:36
  • 1
    Adding zero padding by any amount expands the domain of the original function, which decreases the Nyquist frequency, so yes, the results will be different vectors, but represent two samplings of the same function. – aschepler Jan 25 '21 at 00:21

0 Answers0