How to do a fast Fourier transform (fft) in MATLAB to calculate the spectrum of data from a mat file

  Рет қаралды 28,333

Mathias Magdowski

Mathias Magdowski

Күн бұрын

In this short video, I explain how to import a given mat file with raw data in MATLAB, how to extract time steps and numerical function values from the given matrices, and how to plot the time domain data. After checking the validity of the data (no NaN values), setting the first time step to zero, and making sure that all time steps are equidistant, I show how to calculate the frequency spectrum and the corresponding frequency values using the given 'fourier' function. Finally, I demonstrate how to plot the amplitude spectrum with a linear and logarithmic axes scaling.
MATLAB source code of the example:
cloud.ovgu.de/s/yrmZK2BCMW7t6KH
In the video, I forgot to show how to check for equidistant time steps. If you run
plot(diff(t))
you should get a constant line, which is the case here.
Chapter marks:
0:00 Loading the first matrix
2:01 Loading the second matrix
3:34 Removing the time offset
4:41 Adding axis labels
5:27 Looking at the time function
7:00 Fast Fourier transform
9:40 Looking at the spectrum
10:53 Double-logarithmic axes scaling
12:31 Some more advice

Пікірлер: 87
@user-bg6og7kh8y
@user-bg6og7kh8y 2 жыл бұрын
Yank you so much for you Code , that helps ma a lot expecially for the people lacking of coding experiences.🥰
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
You are welcome, I' glad that you found the video useful.
@masoncondren4488
@masoncondren4488 Жыл бұрын
Thank you so much for the help! Is there anyway I could reference your coding for a research paper I am writing?
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
Each reference should have an author (Magdowski, Mathias), a title ("How to do a fast Fourier transform (fft) in MATLAB to calculate the spectrum of data from a mat file"), a year of publication (2021) and a way how to access it, e.g. a URL (kzfaq.info/get/bejne/obt7pNWVktK-cp8.html or cloud.ovgu.de/s/yrmZK2BCMW7t6KH or kzfaq.info). I think this should be okay.
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
Here is another student question that reached me by e-mail, which I will reproduce here: "I had seen your video downloaded fourier.m function. I then applied it to my measurement data for a project. However, when I evaluated it, I was a bit confused because the DC component seemed too high. A test showed that the DC component in the spectrum is shown twice as large as it actually is. In the case of the example, the sine is shifted by 2, but in the spectrum at a frequency of 0, a DC component of 4 is displayed. If you have a moment, I would be glad if you could take a look at it. Here is my test code:" x=linspace(0,2*pi,400); y=10*sin(3*x)+2; figure plot(x, y) [f,X_f]=fourier(x,y, 'sinus'); figure plot(f, abs(X_f))
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
And here is my answer: Thanks for asking. This is not a bug, it is a feature. "See also: en.wikipedia.org/wiki/Fourier_series#Common_forms The FFT calculates the double DC component a_0. The factor 2 is then considered as a divisor under a_0 in the formula. The DC component is then a_0/2. PS: The FFT becomes clearly better, if one omits the last point in the definition range and the sine function has then no jump when "putting the values one behind the other". See my version of the script:" x=linspace(0,2*pi,400); % remove last value x=x(1:end-1); y=10*sin(3*x)+2; figure plot(x,y) [f,X_f]=fourier(x,y,'sinus'); figure plot(f,abs(X_f))
@xiadu3539
@xiadu3539 2 жыл бұрын
Hi, thanks for the tutorial. After the plot, the y value is in dB or in absolute value? And how to plot it in dB vs Frequency? Thank you.
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
Thanks for your inquiry. After plotting, the y value or amplitude is still an absolute value in linear units, even if plotted on a logarithmic scale. If you want to convert this amplitude into decibels or dB, you need to do this conversion: en.wikipedia.org/wiki/Decibel#Root-power_(field)_quantities Does this answer your question?
@xiadu3539
@xiadu3539 2 жыл бұрын
@@MathiasMagdowski Thank you so much for your quick reply. Could you please check if my code is correct as follows? [f_CT,M_CT] = fourier(T,ID_CT_NG,'sinus'); plot(f_CT,20*log(abs(M_CT)),'blue'); Does this work? thank you so much!
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
@@xiadu3539 This works not really, as the decibel is defined using the decadic logarithm, i.e. the logarithm with the base of 10. The corresponding function is called log10 and not log in MATLAB.
@aaroof1361
@aaroof1361 Жыл бұрын
Hello Prof, Thank you for this wonderful tutorial. I try to plot the fourier spectrum of my data using your code but I receive this feedback as error: 'Check for incorrect argument data type or missing argument in call to function 'fourier''. What could possibly be the reason? I used this syntax: [f,s_f]=fourier(t,s,'sinus'); A quick reply is much appreciated. Thank you
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
If you obtain an error message stating that the argument data type is incorrect or the argument is missing, you should check if both if your argument variables t and s exist and what their data type is. You should be able to locate these variables in the MATLAB workspace and see their data type there. The data type should be a plain double-precision vector or array, not a cell array, table or something like this.
@aaroof1361
@aaroof1361 Жыл бұрын
@@MathiasMagdowski thanks for your prompt reply. I figured it out. It had to do with way I loaded my data. I tweaked it a bit and everything was fine. Thank you very much 😊
@hadinaderi7342
@hadinaderi7342 Жыл бұрын
If we have periodic waveforms with different periods and we want to plot their FFT in one diagram, should we consider for each individual waveform only one period data in the time domain?
@hadinaderi7342
@hadinaderi7342 Жыл бұрын
Or this is realised using the introduced function automatically.
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
​@@hadinaderi7342 No, this is not done automatically by the provided fourier.m function. In general, for periodic functions (where the periodic time is well known and stable), it is good advice to always use a time range for the input of the FFT that is a multiple of the periodic time. You should keep in mind that the FFT assumes that the input signal always repeats or replicates itself consecutively. Therefore, the last time step of one time period should be omitted for the input so that the first time step of the next virtual replica can directly follow (without introducing a non-existing step, jump or sharp bend). In addition, one should consider that the time step of the time function corresponds to the highest frequency in the spectrum while the time range of the time function corresponds to the frequency step in the spectrum. I hope this helps.
@giulianosalerno1203
@giulianosalerno1203 3 ай бұрын
Great function!Could you do something like this to calculate the psd?
@MathiasMagdowski
@MathiasMagdowski 3 ай бұрын
Thanks for your question. By 'psd', you mean the 'power spectral density' (see: en.wikipedia.org/wiki/Spectral_density#Power_spectral_density), right? Maybe I could also record a video for this. To be honest, I have never really used the power spectral density before. Nevertheless, there are existing MATLAB functions readily available for this, see: de.mathworks.com/help/signal/ug/spectral-analysis.html
@giulianosalerno1203
@giulianosalerno1203 3 ай бұрын
@@MathiasMagdowski Hopefully you had already worked with power spectral density (PSD). If you make a video on this it will certainly clarify. Thank you!
@MathiasMagdowski
@MathiasMagdowski 3 ай бұрын
​@@giulianosalerno1203Maybe, but certainly not within the next weeks as I am busy with conferences, international teaching exchange programs and distinguished lecturer talks.
@giulianosalerno1203
@giulianosalerno1203 3 ай бұрын
Could you please clarify better the difference in the FFT measurement unit in the "pulse" or "sinus" case?
@MathiasMagdowski
@MathiasMagdowski 3 ай бұрын
Thanks for your inquiry. The difference between the two modes 'pulse' and 'sinus' for the fourier.m function is also quite well documented within the source code of the function itself. The purpose of the 'pulse' is to calculate the Fourier transform of a single, non-repetitive pulse or an impulsive signal. In contrast, the idea of the 'sinus' mode is to calculate the Fourier transform of a continuous, repetitive signal like a pulse train. The 'pulse' mode corresponds to the common Fourier transform (en.wikipedia.org/wiki/Fourier_transform) whose result is a continous frequency spectrum or a density spectrum. If the time-domain function has for the example the unit V (volt), the density spectrum will have the unit V/Hz (volt per hertz). In contrast, the 'sinus' mode corresponds to the Fourier series (en.wikipedia.org/wiki/Fourier_series) whose result is a discrete frequency spectrum. Correspondingly, this spectrum will also have the unit V if the time function has the unit V. The main difference between the two modes inside the function 'fourier.m' is that a different scaling or renormalization of the amplitude is used.
@albacabanillaspascual276
@albacabanillaspascual276 Жыл бұрын
Hi, i have a 3D data matrx tha contains (channel x time x trials), i have to calculate the power spectrum potential of each trial and each channel. Then i have to calculate de power spectrum of all the trials of each trials. How can i do? Thank you very mucho for your video
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
Sorry for the delayed answer. I would suggest to run a for-loop over the channels and trials to calculate the corresponding spectrum separately. I'm not sure what you mean by the "spectrum of all the trials of each trials". Maybe you can explain this a little further. Is the average over all trials meant there or maybe the corresponding maximum? You may also send me your MATLAB code and some sample data via e-mail to: mathias.magdowski@ovgu.de
@jasonzimmermann9860
@jasonzimmermann9860 2 жыл бұрын
Hello and Thank you for the Tutorial! After using the fourier command, there come the error Message „Check for missing Argument or incorrect Argument data type in call to function ‚fourier‘. Do i Need a Special Tool Box or what can i do for activate this matlab command? Thank you very much!
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
Dear Jason, thanks for your inquiry. You can find the corresponding fourier.m function in a ZIP file, which is linked in the video description: cloud.ovgu.de/s/yrmZK2BCMW7t6KH Does this help and answer your question?
@jasonzimmermann9860
@jasonzimmermann9860 2 жыл бұрын
Dear Mr. Dr. Magdowski, thank you very much for the fast respond it was helpful! With best regards Jason
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
@@jasonzimmermann9860 You are welcome.
@sandeepvinnakota1552
@sandeepvinnakota1552 Жыл бұрын
hello professor, Greetings of the day! " I want to do the FFT analysis for the data which is generated by simulation software that contains two columns (time steps with a difference of 0.00625 )in the first column and the no.of particles at the particular time step in the second column. in the simulation, the flow is at a frequency. I want to visualize whether the particles are following the same path of flow. so I want to see the dominant frequency using FFT. will that be possible?"
@sandeepvinnakota1552
@sandeepvinnakota1552 Жыл бұрын
i can provide you more details with the generated data if needed.
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
@@sandeepvinnakota1552 Thanks for your inquiry and sorry for the delay in communication, but I was on vacation and on business travel. If you have a time-dependent function, e.g. the number of particles as a function of time, you can apply the Fourier transform and get the corresponding spectrum of this time function. This will allow you to analyse the frequency-dependent behavior of this process. For example, you may also observe dominant frequencies, if they exist. In contrast, I assume that it might be difficult to analyse, whether the particles are following a similar path, if this question is not already answered in your time-domain data. If you like, you may send me your well-described data set and your well-document MATLAB code to mathias.magdowski@ovgu.de and I might have a look.
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
Here is another question, which I will reproduce here: "Can we do the FFT of a three-axis accelerometer? I am new to MATLAB and engineering. I have the data but I am really confused even after checking the internet. From your example, there is a column for time t, but in my data, there is no t but only x, y and z data." Here is my answer: There is a specific video about accelerometer data. You can find it here: kzfaq.info/get/bejne/i5ahlqeqspzJZqs.html If you have no time values given, but if your accelerometer data is sampled at equidistant time steps (called t_step in a time unit like second) using a specific sampling frequency (called f_sample in a frequency unit like hertz, where t_step = 1/f_sample or f_sample=1/t_step), you can create the time vector t yourself. First variant, where t_max is the maximum time of your sampling period: t=0:t_step:t_max; Second variant, where N_steps is the number of time steps or sampling points: t=linspace(0,(N_steps-1)*t_step,N_steps);
@Richard-ng2ch
@Richard-ng2ch 2 жыл бұрын
Thanks, but I get "Check for incorrect argument data type or missing argument in call to function 'fourier'." error for the [f,X_f] line
@Richard-ng2ch
@Richard-ng2ch 2 жыл бұрын
Edit, Oh I am a beginner and didn't realise I had to put the source code files into the same folder as the written program. All sorted now, thank you very much! This has worked much better than the other fft tutorials, and also I am fft'ng a pulse signal so could use the pulse option, happy! :-)
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
@@Richard-ng2ch No problem, you are welcome.
@masoncondren4488
@masoncondren4488 Жыл бұрын
I was able to complete the transformation, but it is giving me 500 Hz as the max in the FFT data when I gathered at a frequency of 2000 Hz and a max of 250 Hz when I gathered at a frequency of 1000 Hz. Is this accurate or do I need to adjust the coding? I figured out my problem, my timestep did not begin at 0, changing that fixed the calculations.
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
Okay, very good. Here is a simple MATLAB code example that shows that for e.g. 2000 Hz sampling frequency, the highest frequency in the spectrum corresponds to 1000 Hz, just like one would expect due to the Nyquist-Shannon sampling theorem (en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem): f_sample=2e3 f_signal=100 t_step=1/f_sample T_period=1/f_signal t=0:t_step:T_period-t_step x_t=5*cos(2*pi*f_signal*t) figure(1) plot(t,x_t) xlabel('time in s') ylabel('signal') [f,X_f]=fourier(t,x_t,'sinus') figure(2) plot(f,abs(X_f)) xlabel('frequency in Hz') ylabel('amplitude spectrum')
@vivienne1592
@vivienne1592 6 ай бұрын
hello, prof, I have a question, I use the functions fourier.m but I can't understand this function, Do you have a video description for it? thank you very much !
@MathiasMagdowski
@MathiasMagdowski 6 ай бұрын
Thanks for the question. I don't have such video (yet), but I could record one for sure. Which part of the function is most unclear? There are also some comments in the fourier.m function. Which of them are difficult to understand?
@vivienne1592
@vivienne1592 6 ай бұрын
hello,prof Thank you for your reply. In the function fourier.m, I have two questions: 1.the first one is " XX(1:N)=2/N*fft(x_t)", I can't understand it , for exemple, when I modify '2' to '2.5' in this sentence, the simulation of the image perfectly coincides with the correct image. 2.the second is " X_f(1:N/2+1)=XX(1:N/2+1)". I can't understand this sentence. Thank you very much. @@MathiasMagdowski
@MathiasMagdowski
@MathiasMagdowski 6 ай бұрын
@@vivienne1592 Thanks for your reply. 1. If you change the factor 2 in the formula XX(1:N)=2/N*fft(x_t) to another value, e.g. 2.5, the shape of the spectrum will of course remain the same, because you change all the values of the array by a common factor. Still, the amplitudes given in the spectrum will not fit to the amplitude of the oscillations in the corresponding time function anymore. 2. The ordering of the frequencies in the output vector of the basic 'fft' function is kind of strange. The first entry in the spectrum obtained by the 'fft' function refers to the doubled constant part of the transient signal. It is followed by the positive frequencies in ascending order up the the highest frequency. Then, there are the negative frequencies in descending order. 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. Exactly this is done by the statement X_f(1:N/2+1)=XX(1:N/2+1). If I have time tomorrow, I might record a corresponding video with an explanation.
@MathiasMagdowski
@MathiasMagdowski 6 ай бұрын
The requested video is now finished: "How to scale the result of the fft (fast Fourier transform) and how does fourier.m work in MATLAB?" kzfaq.info/get/bejne/nqeSptKJxsCVZGg.html
@vivienne1592
@vivienne1592 6 ай бұрын
Thank you very much! For 1, I can't understand, how to explain....my prof simulate the spectum with his script and I simulate the spectum with your script and my data.I known 2/N is a formula for normalisation... If I do not change the factor 2, the amplitude always different but I don't know if changing the factor from 2 to 2.5 is the correct or not, because in my simulation with the factor 2.5, my result plots and plots of my prof are completely consistent. @@MathiasMagdowski
@mandarkadwekar6327
@mandarkadwekar6327 2 жыл бұрын
Hello, I am currently doing PIV analysis and would like your help plotting velocity at certain point over the time I have a mat file with generated data from cross correlation. I am not able to code fft for this purpose no matter how hard I think.
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
Thanks for your question. As far as I understand your problem of particle image velocimetry (en.wikipedia.org/wiki/Particle_image_velocimetry), you have pictures with the positions of particles and want to calculate their velocity, which is the time derivative of the position. What do you need a Fourier transform or FFT for? Which frequency spectrum of which signal do you want to calculate?
@mandarkadwekar6327
@mandarkadwekar6327 2 жыл бұрын
@@MathiasMagdowski Yes, that is exactly what I meant by PIV. So, I have already processed the images and as a result I have a matrix of velocity values at different point in the picture. I want to use this matrix of velocity component to do a fft and plot it over time for any specific point in the image. I hope I was able to explain my problem properly.
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
@@mandarkadwekar6327 Thanks for the clarification. For me, it is still not clear what you would like to achieve. The result of the FFT will be a spectrum, i.e. the amplitudes and phases of sinusoidal signals with different frequencies. So the results of the FFT will be a function of frequency, not of time. If you want to do an FFT analysis and still plot something over time, you need to consider something like a short-time Fourier transform (en.wikipedia.org/wiki/Short-time_Fourier_transform). Nevertheless, there will always be a trade-off between frequency and time resolution.
@mandarkadwekar6327
@mandarkadwekar6327 2 жыл бұрын
@@MathiasMagdowski Hello thanks for your reply. Somehow I was confused while applying the code but now it is clear to me. I applied the source code now, but when I plot the function vs frequency graph, I am getting frequency for values that are higher than the maximum value in the original function matrix.
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
@@mandarkadwekar6327 Thanks for your further inquiry. I'm glad to hear that the MATLAB code is working for you. Nevertheless, I do not exactly understand your problem. What do you mean by "frequency for values that are higher than the maximum value in the original function matrix"? What is meant by "values", frequency values or amplitude/magnitude values? Again, as the results of the FFT or Fourier transform, you will get amplitudes and phases as a function of frequency, not the "frequency for values". What is further meant be the "original function matrix"? Why is it a matrix and not just a vector?
@fluminox13
@fluminox13 6 ай бұрын
Hi, I think they redone the comand, becuase I get the error: Incorrect number or types of inputs or outputs for function fourier. Error in fft_example (line 26) [f,X_f]=fourier(t,x_t,'sinus'); Is there a way to fix this?
@MathiasMagdowski
@MathiasMagdowski 6 ай бұрын
Thanks for your question. Have you also used the fourier.m function from the ZIP file provided via the download link (cloud.ovgu.de/s/yrmZK2BCMW7t6KH) in the video description? This function takes two arguments, the time steps and the corresponding values of the time function. An optional third argument specifies the scaling of the spectrum. If you have another file or function present in your MATLAB workspace that is called fourier.m, this might lead to some confusion.
@fluminox13
@fluminox13 6 ай бұрын
@@MathiasMagdowski yes, First I followed your tutorial with my own numbers and got the error. Than I downloaded your zip from under the video and got the same error. I can try the link you send me now later
@MathiasMagdowski
@MathiasMagdowski 6 ай бұрын
@@fluminox13 Okay, feel free to come back to me at any time.
@ahmedbadawy9520
@ahmedbadawy9520 Жыл бұрын
I have an array of time but it’s not uniform. So according to the requirements it must be equidistant.How can I make them equidistant? And if I do so wouldn’t I get less points
@ahmedbadawy9520
@ahmedbadawy9520 Жыл бұрын
Can I use linespace(start, end, no of points)?
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
If the timesteps are not equidistant, you need to interpolate them to equidistant time steps, e.g. using the interp1 function built into MATLAB, see: www.mathworks.com/help/matlab/ref/interp1.html You don't lose points or drastically lower the accuracy, if the equidistant (or uniform) time step of the interpolated time function is in the same range as the smallest time step of your non-uniform time steps. The linspace function is indeed useful to create equidistant time steps: t_uniform=linspace(0,t_end,N_steps); You can then interpolate your time function values: x_t_uniform=interp1(t_nonuniform,x_t_nonuniform,t_uniform); I hope this answers the question.
@ahmedbadawy9520
@ahmedbadawy9520 Жыл бұрын
Thank you for your quick response. I tried to get the equidistant time step using the linspace() with start and end time but when I checked with plot(diff(time)) I didn’t get a line. I plotted the fft regardless but I am not sure the output (frequencies) is right. Would be great if you can have a look at my data.
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
@@ahmedbadawy9520 Sure, you may send me your data to my OVGU e-mail address.
@sherifkhairy2879
@sherifkhairy2879 Жыл бұрын
i want to writ a code for fft with 60 hz fundamental frequency to get the abs and angle in degree for further electrical calculation , in other words i want to build a code just as the fft simulink block that take the voltage or current signal and convert them for further impedance calculation. i hope you understand my question, thanks in advance
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
Thanks for your question. Your task should not be too difficult to solve. If you want to get the amplitude and phase of the spectral component or fundamental frequency of your signal at a frequency of 60 Hz, your need to sample your time-domain signal with a sample rate of at least twice of this, so 120 Hz, but better with the 10-fold or even 100-fold of this frequency, so 600 Hz to 6000 kHz (corresponding to 6000 samples per second). You must also sample for at least one periodic time of this frequency of 60 Hz, so at least for 16.6 ms. Still, is is better to sample longer, up to 10 or even 100 periodic times, so 166.6 ms or even 1.666 s. Then you apply the FFT algorithm as explained in the video using the fourier.m function and just use the 60 Hz-component from the resulting frequency spectrum. I hope this helps. If you have further questions, please let me know. Best regards from Israel.
@sherifkhairy2879
@sherifkhairy2879 Жыл бұрын
Thank you, another question , i want to calculate the current and voltage each sample to be able to detect when the disturbance occures for fault analysis, in another word i what to build for loop that calculate the impeadance from current and voltage each sample to monitor the change grahpically from the given rawdata
@MathiasMagdowski
@MathiasMagdowski Жыл бұрын
​@@sherifkhairy2879 Thanks for your further inquiry. Impedance is something that is only defined for (complex-valued) phasors of voltage and current, see en.wikipedia.org/wiki/Electrical_impedance Therefore, if you have a time-dependent voltage and time-dependent current function, you need to calculate their respective spectrum first. From the frequency-dependent voltage and current phasors, you can then calculate a frequency-dependent impedance. From this you might monitor some change in the behavior of your system for the detection of some occurence of disturbance or fault analysis.
@tsehayenegash8394
@tsehayenegash8394 2 жыл бұрын
I have time series data and I did the program like this: 1. Temp=xlsread('Addis_Ababa_wholeRS.xlsx',4); 2. Height=Temp(:,1); 3. Temperature=Temp(:,2:end); 4. m=Temperature; 5. N=length(m); 6. fs=1; % sampling frequency 7. subplot(2,1,1) 8. plot(Temperature) 9. title('\bf Temp Variation over Addis Ababa station during 2006 - 2020 at 2 (Km)') 10. ylabel('Amplitude') 11. xlabel('Month') 12. mf=fft(m); % fast Fourier transform 13. ps=abs(mf).^2; % power spectrum density 14. fa=(0:N/2).*(fs/N); % analysis frequency 15. subplot(2,1,2) 16. plot(fa,ps(1:length(fa))) 17. title('\bf Temp Peaks for Addis Ababa station during 2006 - 2020 at 2 (Km)') 19. ylabel('Power Spectrum') 20.xlabel('Frequency') What I need your help is how can I plot anlyze frequency vs amplitude meaning instead of this plot(fa,ps(1:length(fa))) to plot(fa, aAmplitude). Thank you.
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
Dear Tsehaye Negash, thanks for your further inquiry. There are some issues: 1. As I don't have your Microsoft Excel file "Addis_Ababa_wholeRS.xlsx", I cannot check your proposed MATLAB code. You may send it to me via mathias.magdowski@ovgu.de 2. Why do you not use the provided functions fourier.m and invfourier.m from cloud.ovgu.de/s/yrmZK2BCMW7t6KH, but try to use the more basic fft and ifft functions, 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. I don't really get your question? What shall the variable 'aAmplitude' mean or include? 4. Typically, one would like to plot amplitude vs. frequency, so the frequency on the x-axis or abscisssa and the amplitude on the y-axis or ordinate. Do you really want to plot the 'frequency vs amplitude'?
@jianyuhua
@jianyuhua 2 жыл бұрын
why don't you use fft function?
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
The 'fft' function (fast Fourier transform or discrete Fourier transform) is used within the 'fourier' function, which works like an API (application programming interface) or a wrapper to the underlying 'fft' function. The problems or issues with the basic 'fft' function are the following: 1. The 'fft' function only takes one argument as input, the values of the time function, and returns one output variable, the values of the spectrum. 2. Therefore, you have to take care about the corresponding time steps and frequency values yourself. The highest frequency corresponds to the shortest time, while the smallest frequency corresponds to the longest time. The 'fourier' function takes care of this. As a second input argument, you input corresponding time (equidistant) steps and get back the corresponding (equidistant) frequencies as a second output vector. 3. The ordering of the frequencies in the output vector of the basic 'fft' function is kind of strange. The first entry in the spectrum obtained by the 'fft' function refers to the doubled constant part of the transient signal. It is followed by the positive frequencies in ascending order up the the highest frequency. Then, there are the negative frequencies in descending order. 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. 4. The amplitude or magnitude of the spectrum calculated by the basic 'fft' function depends on the number of steps or vector length of the given time function. The wrapper provided by the 'fourier' function takes care of this and renormalizes the amplitude for two different cases, periodic functions (like sums of sine and cosine functions, rectangular pulse trains or clock signals) or aperiodic functions (like single pulses).
@kunalreddy434
@kunalreddy434 2 жыл бұрын
what can i do if i have timestamp instead of time as a cloumn?
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
Thanks for the question. What do you exactly mean by a timestamp? Something like "2022-03-25 21:20:34.56"? If yes, you need to convert the timestamps into a vector of plain time steps with the unit second. Remember that the first time given in this time vector must be zero and that all other time steps must be equidistant. If they are not equidistant, you need to interpolate them to equidistant time steps, e.g. using the interp1 function.
@kunalreddy434
@kunalreddy434 2 жыл бұрын
I am not able to figure out how to convert it into a vector of seconds.. you think you could help with that?
@MathiasMagdowski
@MathiasMagdowski 2 жыл бұрын
@@kunalreddy434 I can try. Please send me your data file(s) and your current MATLAB code to my e-mail address mathias.magdowski@ovgu.de!
@kunalreddy434
@kunalreddy434 2 жыл бұрын
Sure! Thanks a lot
@kunalreddy434
@kunalreddy434 2 жыл бұрын
Any luck sir?
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 20 МЛН
когда повзрослела // EVA mash
00:40
EVA mash
Рет қаралды 3,4 МЛН
⬅️🤔➡️
00:31
Celine Dept
Рет қаралды 52 МЛН
Understanding the Discrete Fourier Transform and the FFT
19:20
But what is the Fourier Transform?  A visual introduction.
20:57
3Blue1Brown
Рет қаралды 10 МЛН
The Fourier Series and Fourier Transform Demystified
14:48
Up and Atom
Рет қаралды 787 М.
Fourier Transforms FFT in MATLAB | MATLAB Tutorial
24:03
Phil Parisi
Рет қаралды 33 М.
The Fast Fourier Transform (FFT): Most Ingenious Algorithm Ever?
28:23
Denoising Data with FFT [Matlab]
10:34
Steve Brunton
Рет қаралды 81 М.
Using Matlab's fft function
17:40
David Dorran
Рет қаралды 432 М.
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 20 МЛН