Testing my Discrete Fourier Transform program

In summary: 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...
  • #1
john kly
8
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
  • #2
You can use Excel to test it. Just do the same DFT in Excel and compare the results. :smile:
 
  • #3
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.
 
  • #4
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
  • #5
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
 
  • #6
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: 890
  • #7
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
 
  • #8
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?
 
  • #9
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

  • 200mVp100Hz_Div2.pdf.pdf_VfsCalcs.pdf
    612.7 KB · Views: 382
  • Readme Howto Work with CSV data files from metroASICTest.txt
    1.9 KB · Views: 557
  • PF FFT Plots.xlsx
    33.4 KB · Views: 334
  • #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 :)
 

FAQ: Testing my Discrete Fourier Transform program

How do I know if my Discrete Fourier Transform program is accurate?

The best way to ensure accuracy is to compare the results of your program with those of a known and validated DFT program. You can also test your program on a variety of input signals and compare the outputs to expected results.

What is the best way to test my Discrete Fourier Transform program?

The best way to test your program is to use known input signals with known DFT outputs and compare the results. You can also test your program on a variety of input signals to ensure it is robust and can handle different types of data.

How can I optimize my Discrete Fourier Transform program?

To optimize your program, you can use efficient algorithms and data structures, such as the Fast Fourier Transform (FFT) algorithm and arrays, to reduce the time and memory required for calculations. You can also analyze and improve the complexity of your program to make it more efficient.

How do I handle edge cases in my Discrete Fourier Transform program?

It is important to consider and test for edge cases, such as zero-padding and aliasing, in your program. You can handle these cases by implementing appropriate checks and conditions in your code to prevent errors and ensure accurate results.

How can I improve the speed and efficiency of my Discrete Fourier Transform program?

To improve the speed and efficiency of your program, you can use parallel programming techniques, such as multi-threading, to distribute the workload across multiple processors. You can also optimize your code and use efficient algorithms and data structures to reduce the time and memory required for calculations.

Similar threads

Replies
17
Views
2K
Replies
2
Views
5K
Replies
6
Views
2K
Replies
4
Views
1K
Replies
2
Views
3K
Replies
3
Views
1K
Replies
7
Views
3K
Back
Top