Testing my Discrete Fourier Transform program

john kly
Messages
7
Reaction score
1

Homework Statement


I've written a program that calculates the discrete Fourier transform of a set of data in FORTRAN 90. To test it, I need to "generate a perfect sine wave of given period, calculate the DFT and write both data and DFT out to file. Plot the result- does it look like what you expect analytically?" The problem is I don't understand how to do this. The result from the DFT is an array of length N of complex numbers, so I've taken the absolute value. My problem is with testing it, I don't understand what I'm supposed to plot the result against, and what it should look like.

Homework Equations



None.

The Attempt at a Solution


Here is my code:

  1. program DFT
  2. implicit none
  3. integer :: k, N, x, y, j, r, l, istat
  4. integer, parameter :: dp = selected_real_kind(15,300)
  5. real, allocatable,dimension(:) :: h, fin
  6. complex, allocatable, dimension(:) :: rst
  7. complex, dimension(:,:), allocatable :: W
  8. real(kind=dp) :: pi, z, P, A, i
  9. pi = 3.14159265359
  10. P = 2*pi
  11. A = 1
  12. !open file to write results to
  13. open(unit=100, file="dft.dat", status='replace')

  14. N = 400
  15. !allocate arrays as length N, apart from W (NxN)
  16. allocate(fin(N))
  17. allocate(h(N))
  18. allocate(rst(N))
  19. allocate(W(-N/2:N/2,1:N))

  20. pi = 3.14159265359
  21. !loop to fill the sample containing array
  22. do k=1,N
  23. h(k) = sin((2*k*pi)/N)
  24. end do
  25. !loop to fill the product matrix with values
  26. do j = -N/2,N/2
  27. do k = 1, N
  28. W(j,k) = EXP((2.0_dp*pi*cmplx(0.0_dp,1.0_dp)*j*k)/N)

  29. end do
  30. end do
  31. !use of matmul command to multiply matrices
  32. rst = matmul(W,h)
  33. print *, h, w
  34. fin = abs(rst)

  35. write(100,*) fin

  36. end program
Any help is appreciated, thanks.
 
Physics news on Phys.org
You can use Excel to test it. Just do the same DFT in Excel and compare the results. :smile:
 
berkeman said:
You can use Excel to test it. Just do the same DFT in Excel and compare the results. :smile:
Hi,
thanks for the suggestion, but it's for an assignment and so we've been told to perform this specific test as part of the assigment.
 
If the original signal is a function of time, then the DFT will give you a function of frequency.

I would plot the real and the imaginary parts, instead of the absolute value, since the latter could be correct even though your phase wouldn't.
 
  • Like
Likes berkeman and john kly
DrClaude said:
If the original signal is a function of time, then the DFT will give you a function of frequency.

I would plot the real and the imaginary parts, instead of the absolute value, since the latter could be correct even though your phase wouldn't.

Ok thank you, how do I plot both the real and imaginary parts of an array? I suppose you mean, say an element is x + iy, I'm plotting x vs y for every element? I'm not sure how to go about this.

Thanks again
 
john kly said:
how do I plot both the real and imaginary parts of an array? I suppose you mean, say an element is x + iy, I'm plotting x vs y for every element? I'm not sure how to go about this.
https://www.ini.uzh.ch/~ppyk/BasicsOfInstrumentation/matlab_help/math/fftgui4.gif
fftgui4.gif
 

Attachments

  • fftgui4.gif
    fftgui4.gif
    4.1 KB · Views: 933
berkeman said:
https://www.ini.uzh.ch/~ppyk/BasicsOfInstrumentation/matlab_help/math/fftgui4.gif
View attachment 221126
I assume here fft means fast Fourier transform? I'm not sure if that's different, or if I should be expecting the same result. If so, then now I know what to expect, so thanks :) but I'm still stuck on trying to plot the real components of the array and the complex ones, and whether or not they should be plotted against each other, or if they should both individually be plotted against something else.

Thanks
 
john kly said:
'm still stuck on trying to plot the real components of the array and the complex ones, and whether or not they should be plotted against each other, or if they should both individually be plotted against something else.
Plotting against each other doesn't make any sense to me. Just plot them against their indexes. In the time domain the steps are discrete time steps, and in the frequency domain, they are discrete frequency steps, no?
 
berkeman said:
Plotting against each other doesn't make any sense to me. Just plot them against their indexes. In the time domain the steps are discrete time steps, and in the frequency domain, they are discrete frequency steps, no?
Thank you, can you just clarify what you mean by their indexes?
 
  • #10
Well, discrete functions have indices, no? Discrete functions in the time domain have indices that are some multiple of the time step. Discrete functions in the frequency domain have indices that are some multiple of the frequency step.

I'll post an example Excel spreadsheet from a recent project with a time domain and frequency domain DFT pair tomorrow when I'm back at work. I was looking for noise sources in some low-level ADC measurements in that project...
 
  • #11
berkeman said:
Well, discrete functions have indices, no? Discrete functions in the time domain have indices that are some multiple of the time step. Discrete functions in the frequency domain have indices that are some multiple of the frequency step.

I'll post an example Excel spreadsheet from a recent project with a time domain and frequency domain DFT pair tomorrow when I'm back at work. I was looking for noise sources in some low-level ADC measurements in that project...
Great, thank you very much for your help! :)
 
  • Like
Likes berkeman
  • #12
berkeman said:
I'll post an example Excel spreadsheet from a recent project with a time domain and frequency domain DFT pair tomorrow when I'm back at work. I was looking for noise sources in some low-level ADC measurements in that project...
I'm attaching several files from the ADC characterization that I mentioned. The first is a PDF showing the digitized input waveform and the resulting FFT that I computed with Excel. There are hand-written notes on the plots showing the scales involved in both the time domain and the frequency domain.

The 2nd file is the source Excel file that generated the plots, and the 3rd file is a TXT file with notes to myself about how to bring in the CSV data file and manipulate it in Excel to get the plots. Hope this material is of help.
 

Attachments

  • #13
berkeman said:
I'm attaching several files from the ADC characterization that I mentioned. The first is a PDF showing the digitized input waveform and the resulting FFT that I computed with Excel. There are hand-written notes on the plots showing the scales involved in both the time domain and the frequency domain.

The 2nd file is the source Excel file that generated the plots, and the 3rd file is a TXT file with notes to myself about how to bring in the CSV data file and manipulate it in Excel to get the plots. Hope this material is of help.
Hi, thank you very much for this! I will look through it all now :)
 

Similar threads

Back
Top