How to do a discrete Fourier transform (FFT) and to plot the amplitude and phase spectrum in MATLAB

Поделиться
HTML-код
  • Опубликовано: 26 авг 2024

Комментарии • 42

  • @bilalbadar7310
    @bilalbadar7310 3 года назад +2

    Excellent video to understand fourier transform, it really helps me alot

  • @rezakhoshkbary7975
    @rezakhoshkbary7975 2 года назад +1

    Simply Lovely! This is what I wanted. Amazing. Thanks a lot

  • @senmimivirus
    @senmimivirus 3 года назад +1

    Very useful video, it helped me a lot with my project. :)

  • @jorgepascual3507
    @jorgepascual3507 3 года назад +2

    Really amazing and excellent video, it is very useful.
    Thank you ;)

  • @saapman
    @saapman Год назад +1

    Very nice video. Thanks!

  • @fiskusmati
    @fiskusmati 3 года назад +1

    Great video but I won't say that in engineering world we only have real values for ffts. We often use complex signals (IQ signal), there are cffts (complex ffts) alrotyghms also :D

    • @MathiasMagdowski
      @MathiasMagdowski  3 года назад

      Good comment, but this depends on the representation of the signal. In practice, every physically measurable transient signal (which you can measure as a function of time using an oscilloscope) is a real-valued signal. There is nothing imaginary in time domain. For example, the corresponding IQ signal of a quadrature amplitude modulation is given as s(t) = I(t) · cos(ωt) - Q(t) · sin(ωt). Of course, you can also give this in a complex-valued phasor notation, but then it is already a mathematical model, not a physical representation anymore.

  • @guillermofedericomartinez5809
    @guillermofedericomartinez5809 Месяц назад +1

    Hello Dear Mathias, I can not found the function "fourier", where can I get it? thanks

    • @MathiasMagdowski
      @MathiasMagdowski  Месяц назад +1

      Thanks for your inquiry. The function fourier.m is available in the Octave Online bucket that is linked in the video description.

  • @levuthi
    @levuthi 3 года назад +1

    excellent! Thanks

  • @ranjanrajdahal3557
    @ranjanrajdahal3557 Месяц назад +1

    how can i extrace wave number (k) from a seismic time series data in matlab?
    thanks in advance

    • @MathiasMagdowski
      @MathiasMagdowski  Месяц назад

      Thank you for the question. There are two different definitions of wavenumber, see en.wikipedia.org/wiki/Wavenumber.
      The "spectroscopic wavenumber" is 1/λ, where λ is the wavelength.
      The "angular wavenumber" is 2π/λ. This definition is more common in electromagnetics, my field of research.
      I'm not sure which defintion is typical in seismic analysis.
      In any case, the wavelength λ can be calculated by c/f, where c is the velocity of propagation and f is the frequency. So from you seismic time series, you calculate the amplitude spectrum via Fourier transform. Hopefully, you have some distinct peaks in this spectrum. You take the frequency of these peaks, calculate the corresponding wavelength λ and wavenumber k. I hope this answers your question.

  • @luckyrinaldy2978
    @luckyrinaldy2978 Год назад +1

    hi thank u
    can u give more explanation, why we ignored the negative frequency, many thanks

    • @MathiasMagdowski
      @MathiasMagdowski  Год назад +1

      Negative frequencies only make sense for complex-valued time functions, which usually have no physical meaning in the engineering sciences. For real-valued time functions, the spectrum of the negative frequencies is the complex conjugate of the spectrum of the positive frequencies. Therefore, the negative frequencies can be omitted, as they are redundant and do not contain any relevant information in that case.

  • @duvanfonsecaabril7811
    @duvanfonsecaabril7811 2 года назад +1

    When i try to calculate the spectrum and the corresponding frequency using a discrete Fourier transform, I get this error "Check for incorrect argument data type or missing argument in call to function 'fourier'." could you please help me? Thks a lot for ur video :)

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад

      Thanks for your inquiry, which is difficult to answer without taking a look at your actual code. Can you send me your MATLAB script via e-mail or upload it to octave-online.net/ (or any other cloud storage) and share a link to my e-mail address mathias.magdowski@ovgu.de?

  • @HasanASalbi
    @HasanASalbi 2 года назад +2

    Hi Dr. Magdowski
    I appreciate your video and your clarification,
    I have used your FFT code in other data but the amplitudes of the output spectrum are scaled.
    I have sent you the data and the output results via mail and just need your consultation.
    I will be very grateful if you replied to me.
    Thanks in Advance

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад +1

      Thanks for your inquiry. I was quite busy last week and only today found the time to take a closer look at your code. There are several issues:
      1. You have a signal that contains a transient effect or transient behavior at the beginning within the first cycle of the five displayed cycles. The first cycle is not exactly periodic and equal to the other four following cycles, which show the steady state behavior. You have to know that the fft function assumes that your time signal replicates over and over again. As the transient effect at the beginning does not replicate, is should be omitted from the Fourier analysis.
      2. Your time steps are not equidistant. The fft function only works on equidistant data. Therefore, you need to interpolate your initial time function in order to get equidistant time steps.
      3. You should delete the last time step at the end of the analyzed time function and the signal so that it perfectly replicates itself after the four periods of time without any additional step or jump. If you do so, you will also get your harmonic frequencies at exact multiples of the fundamental frequency that is related to the periodic time of the signal.
      4. Most important: You have modified my fourier function and the corresponding fourier.m file. You have changed line 36 from
      modus='pulse';
      to
      modus='sinus';
      which is somehow okay. In addition, you have changed line 50 from
      if strcmp(modus,'pulse')
      to
      if strcmp(modus,'sinus')
      which of cause changes the behavior of the function with respect to the following lines of code that influence the scaling.
      Just do not modify functions if you don't have a clue what you are doing.
      I have uploaded the corrected code to Octave Online. You can find it here:
      octave-online.net/bucket~5sQWRCXyPWhkTjU48iyRvn
      You will notice that the signal reconstruction now works very well even with just about 10 to 20 harmonics.
      If I find some time I will take your example and make another video out of it.

    • @HasanASalbi
      @HasanASalbi 2 года назад

      @@MathiasMagdowski
      Appreciate your time, assistant, and kindness.
      I really enjoyed seeing more scientific videos on your channel.
      Please, enrich us with your valuable videos.

  • @fitafita2421
    @fitafita2421 2 года назад +1

    I have an exercise can you help me

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад

      Thanks for your inquiry, which is difficult to answer without taking a look at your actual code. Can you send me your MATLAB script via e-mail or upload it to octave-online.net/ (or any other cloud storage) and share a link to my e-mail address mathias.magdowski@ovgu.de or via a reply to this comment?

  • @atduong3917
    @atduong3917 2 года назад +1

    given s(t) = A*sin(2*pi*fc*t). Find Fourier transform of s(t), A =1, fc =5. Can you help me, i'm new to this. Many thanks from Vietnam

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад +1

      Thanks for this question. The answer is already given in your question. The idea of the Fourier transform is to represent a function as a sum of sine functions (with different amplitudes and frequencies). If you are given a sine function s(t) = A*sin(2*pi*fc*t), its Fourier transform will be one (complex) phasor or one component in the frequency spectrum with the respective amplitude A and the frequency fc, i.e. the given sine function. As the Fourier transform is often defined for cosine functions, the phase angle of this component will be -90°, as cos(x-90°) = sin(x).
      You can also try this with the provided 'fourier' function in MATLAB or GNU Octave. Just create meaningful values of the sine function as the corresponding time function and do the fast Fourier transform to calculate the spectral components. Except for the value at fc, these components are all zero. See this example:
      octave-online.net/bucket~8U1MLGxLX2PmD4uHyNapJj

    • @atduong3917
      @atduong3917 2 года назад +1

      @@MathiasMagdowski very thank you for replying me, have a nice day sir.

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад

      @@atduong3917 No problem, you are welcome.

  • @caistead5622
    @caistead5622 2 года назад

    Thank you so much for the video, I am looking to recreate some raw data at an irregular time stamp. Would Dr. Kochetov's code still work?

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад

      Thanks for this nice question. I see different options.
      1. You can do the usual inverse fast Fourier transform to obtain a time function based on equidistant time steps. Afterwards, you use a 1-dimensional interpolation (e.g. linear using the interp1 function in MATLAB) to resample the time function on your irregular time steps.
      2. You only take into account the most relevant spectral components and use the method presentated in this video "How to reconstruct a periodic signal as a sum of cosine functions from the freq. spectrum in MATLAB" (ruclips.net/video/OvyTRxZSolU/видео.html) in order to directly calculate the values of the time function for your irregular time steps.
      Please try it out and let me know if it worked for you. Otherwise you may send me your data and your MATLAB files to my e-mail address so that I can have a look.

    • @caistead5622
      @caistead5622 2 года назад +1

      @@MathiasMagdowski Thanks for getting back to me, I have been trying the second option you recommended but still have made much progress yet :/ but will keep you updated!

    • @caistead5622
      @caistead5622 2 года назад +1

      Hi Mathias, I have tried varying the frequency and other aspects but still cant seem to make it match up, is there any chance you could have a look at the code?

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад

      @@caistead5622 Thanks for your code and sorry for the delay in communication. I was busy and I will try to answer brief. I see several issues with your code:
      1. Your given time steps that you use for the Fourier transform from time domain into frequency domain are not equidistant. Also, the first time step is not zero. Therefore, the Fourier transform will not work. I thought that you already have a frequency spectrum and that you just want to transform this back into a time function with irregular time steps using inverse Fourier transform. To solve this issue, you first need to interpolate your time function on equidistant time steps starting with zero before doing the Fourier transform.
      2. Why do you not use the provided functions fourier.m and invfourier.m, but try to use the more basic fft and ifft, where you have to take care about the frequency and time steps and the renormalization of the amplitudes yourself? Why make it difficult, if it can also be easy?
      3. The arguments of your "time function" are not times in the unit second but lengths in the unit m. The Fourier transform will still work, but therefore, everything is a bit strange and unusual with respect to the units. What is the unit of the time function itself, also meter (for the distance between the pools)?
      4. Your time function looks a bit undersampled and edgy, angular or chiseled, but that might just be a problem of your simplified example.
      5. The first value of your time function and the last value of your time function are not the same, not even close, but they should be quite close to each other, as the FFT and IFFT assume that the time function as well as the spectrum always repeat one after the other, respectively. So there cannot be a big jump between the first value and the last value of the time function as it happens in your case, which is then difficult to reproduce or reconstruct with few harmonics only.
      6. Finally, I also found a bug in the code of my example provided at: octave-online.net/bucket~EP15mJvkgLCmzodFStNw3b
      This code will only work, if the DC part is zero or quite small. If it is not, it must be devided by a factor of 2 when reconstructing the function.
      7. In general, I would not advice to use MATLAB live scripts. They are much slower and less performant that usual MATLAB scripts.

  • @tsehayenegash8394
    @tsehayenegash8394 2 года назад +1

    I appreciate your help. I have 15 years radiosonde data in excell. the radiosonde measured upto 36 km after that its burst. I have few questions
    1. How can I find the sampling frequency? is possible to take 15x12=180 (15 year =180 month) is a sampling frequency?
    2. I plot it month vs height, how can I convert this plot into frquency vs amplitude?
    3. what is the matlab code to convert time series data into frequency?
    4. If you are volentary, I will need your email

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад +1

      My e-mail address is mathias.magdowski@ovgu.de. You can also find my e-mail address in the linked OctaveOnline Bucket (octave-online.net/bucket~CQ8X9mXpy9NaZdF4rSsLQX) in the video description.

    • @tsehayenegash8394
      @tsehayenegash8394 2 года назад +1

      @@MathiasMagdowski Really thanks

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад +1

      @@tsehayenegash8394 Thanks for sending your Excel data file. There are also lots of different value pairs in this file. Which ones are you interested in? What is their corresponding unit (I assume meter for the height and °C for the temperature)?
      Regarding your questions:
      1. 15 years or 180 month is a time duration, not a sampling frequency. The sampling frequency must have the unit of a frequency, which is 1/s, 1/min, 1/h, 1/d, 1/month, 1/year or something like this. Typically, the sampling frequency is the reciprocal of the time step or the sampling time, see: en.wikipedia.org/wiki/Sampling_(signal_processing)
      2. Typically, one would like to plot the amplitude and phase as a function of frequency, not vice versa.
      3. The MATLAB code for my example can be found in the video description as a liked OctaveOnline Bucket (octave-online.net/bucket~CQ8X9mXpy9NaZdF4rSsLQX).
      4. Just for further reference, my e-mail address is mathias.magdowski@ovgu.de

    • @tsehayenegash8394
      @tsehayenegash8394 2 года назад +1

      @@MathiasMagdowski I appreciate your kindness. my problem was solved in the sampling frequency its clear. I attached one year recoreded radisonde data. in the excel sheet the first column is height which is measured in km and the rest column is month ot temperature. the whole year is a huge data so I can expand when I get this year code

    • @MathiasMagdowski
      @MathiasMagdowski  2 года назад

      @@tsehayenegash8394 For easier reference I have put your data file onto our cloud storage:
      cloud.ovgu.de/s/gAPf4jbJMn3y5gn
      The table 'Sheet1' seems to be your raw data, where the height was originally measured in meters and was then rounded to kilometers to have harmonized height references for each measured day. For this purpose, data points also seem to have been averaged for similar height, which might be useful. Nevertheless, the rounding of the height to the nearest integer in km is too coarse in my opinion. It would have been more meaningful to interpolate the arbitrarily height spacing to a fixed height spacing in km, e.g. using a linear or cubic spline interpolation.
      The table 'Sheet2' then only summarizes the data for the rounded heights in km, I assume, but still gives all the measurement for each individual day that was measured. The table 'Sheet3' then only summarizes the averaged values giving one value for each month. This value might be the result of only a single measurement, e.g. in January, or a mean value over 30 days, e.g. in September. The final table 'RS_2006' then summarizes all these values for one year, where the first column gives the height in km, while the other columns give the corresponding temperature in °C for each month. Here, it is unfortunately not very clear, where some of these values come from, e.g. for heights above 10 km for the month of January, where no measurements were taken. Have these temperatures been extrapolated from the lower heights or are the annual averages used here? How meaningful is it, that some temperatures are only exact to one decimal while other are given with up to 12 decimal places or more?
      Despite all these questions, possible difficulties and imprecisenesses, let us assume that the data is meaningful. So we have 12 temperature values as a function of time, one value for each month. Such type of dataset now exists for different heights between 2 km and 36 km. I have plotted these curves in the following file:
      cloud.ovgu.de/s/rNfiFeBmxomscjx
      One can see that the temperature does not really change over the year, at least for most of the heights. The displayed changes seem to be only related to noise or uncertainties of the measurement. So it is of course possible to calculate the amplitude and phase spectrum of these temperatures, but you will observe that the spectrum then also only shows noise, which is to be expected, if the transient signal in time domain mostly consists of noise.
      In addition, a number of just 12 data points is too small to calculate a meaningful spectrum. With just 12 points in time domain, you will only be able to calculate the amplitude and phase for 6 meaningful frequencies in the spectrum. Which useful information would you like to get from these values? So the question is not, if and how it is possible to calculate the frequency spectrum, the question is why one would like to do this and what meaningful information can be obtained from the spectrum.