How to find the frequencies of a time series which contain datetime ?

48 views (last 30 days)
How to find the frequencies of a time series which contain datetime, as in the following example ?
(Note: I did not find such an exmple in the Fast Fourier transform (fft) page)
a = {
'17-Jun-2021 12:00:00', 25
'18-Jun-2021 20:00:00', 21
'19-Jun-2021 06:00:00', 31
'20-Jun-2021 14:00:00', 35
'21-Jun-2021 17:00:00', 31
'22-Jun-2021 16:00:00', 41
'23-Jun-2021 16:00:00', 22
'24-Jun-2021 13:00:00', 22
'25-Jun-2021 15:00:00', 45
'26-Jun-2021 11:00:00', 20
'27-Jun-2021 19:00:00', 25
'28-Jun-2021 18:00:00', 25
'29-Jun-2021 15:00:00', 29
'30-Jun-2021 10:00:00', 31
'01-Jul-2021 15:00:00', 20
'02-Jul-2021 10:00:00', 28
'03-Jul-2021 14:00:00', 31
'04-Jul-2021 13:00:00', 24
'05-Jul-2021 09:00:00', 24
'06-Jul-2021 14:00:00', 25
'07-Jul-2021 15:00:00', 40}
plot(datetime(a(:,1)),cell2mat(a(:,2)))

Accepted Answer

Abderrahim. B
Abderrahim. B on 11 Aug 2022
Edited: Abderrahim. B on 11 Aug 2022
Hi!
You can use pspectrum function. It returns both power and frequency arrays.
% Your data
a = {
'17-Jun-2021 12:00:00', 25
'18-Jun-2021 20:00:00', 21
'19-Jun-2021 06:00:00', 31
'20-Jun-2021 14:00:00', 35
'21-Jun-2021 17:00:00', 31
'22-Jun-2021 16:00:00', 41
'23-Jun-2021 16:00:00', 22
'24-Jun-2021 13:00:00', 22
'25-Jun-2021 15:00:00', 45
'26-Jun-2021 11:00:00', 20
'27-Jun-2021 19:00:00', 25
'28-Jun-2021 18:00:00', 25
'29-Jun-2021 15:00:00', 29
'30-Jun-2021 10:00:00', 31
'01-Jul-2021 15:00:00', 20
'02-Jul-2021 10:00:00', 28
'03-Jul-2021 14:00:00', 31
'04-Jul-2021 13:00:00', 24
'05-Jul-2021 09:00:00', 24
'06-Jul-2021 14:00:00', 25
'07-Jul-2021 15:00:00', 40}
a = 21×2 cell array
{'17-Jun-2021 12:00:00'} {[25]} {'18-Jun-2021 20:00:00'} {[21]} {'19-Jun-2021 06:00:00'} {[31]} {'20-Jun-2021 14:00:00'} {[35]} {'21-Jun-2021 17:00:00'} {[31]} {'22-Jun-2021 16:00:00'} {[41]} {'23-Jun-2021 16:00:00'} {[22]} {'24-Jun-2021 13:00:00'} {[22]} {'25-Jun-2021 15:00:00'} {[45]} {'26-Jun-2021 11:00:00'} {[20]} {'27-Jun-2021 19:00:00'} {[25]} {'28-Jun-2021 18:00:00'} {[25]} {'29-Jun-2021 15:00:00'} {[29]} {'30-Jun-2021 10:00:00'} {[31]} {'01-Jul-2021 15:00:00'} {[20]} {'02-Jul-2021 10:00:00'} {[28]}
% Your plot
plot(datetime(a(:,1)),cell2mat(a(:,2)))
% convert to table, then to timetable
aTbl = cell2table(a) ;
aTbl.Properties.VariableNames = ["Time" , "timeSerie"] ;
aTbl.Time = datetime(aTbl.Time, "InputFormat","dd-MMM-uuuu HH:mm:ss") ;
aTimeTbl = table2timetable(aTbl) ;
% Some preprocessing
aTimeTBL_RETIME = retime(aTimeTbl, "hourly", "previous") ;
% spectrum : returns power and frequency vectors
[pow, freqs] = pspectrum(aTimeTBL_RETIME)
pow = 4096×1
1.0e+03 * 1.4474 1.4449 1.4373 1.4249 1.4077 1.3858 1.3595 1.3291 1.2947 1.2568
freqs = 4096×1
1.0e-03 * 0 0.0000 0.0001 0.0001 0.0001 0.0002 0.0002 0.0002 0.0003 0.0003
% Plot power in log scale vs frequency
figure
semilogy(freqs, pow)
Hope this helps
  10 Comments
dpb
dpb on 12 Aug 2022
Oh, why didn't you say so!!! :) Wouldn't have gotten so uptight on trying to prevent a perhaps egregious blunder... :)
OK, for pedgogical purposes, I'd suggest to look at the sample datasets with MATLAB used in the documentation as starters...there's a sunspot dataset that looks at longer-time calendar data -- although it's not in datetime format (its inclusion having preceded its introduction by 30 years), you could convert the yearly data to datetimes and to a timetable if you wanted practice using the newfangled datetime and other features as well...although truthfully for spectral and time-series analysis, the reversion back to plain doubles is often more convenient because all the signal processing routines haven't been converted to know anything about datetimes or durations. timetable has, but not necessarily the core Signal Processing routines.
As far as figuring out the frequency of the low-sampling frequency signals, to see have it right you can always convert the times to seconds and deal in Hz and then scale the resulting frequencies back to the longer time frames afterwards. It is easy to get confused with days and years in those locations instead, granted.
But, remember and commit to memory the base fundamental relationships in data sampling/signal analysis -- if you remember these, you can always get it right --
Fs = 1/dt - Sampling frequency is 1/delta-t
Fmax = 1/(2*dt) (= Fs/2) - Fmax (Nyquist) is one-half sampling rate
T = N*dt (= 1/df) - Sample time span is number samples * dt
df = Fmax/(N/2) - frequency resolution is Fmax over half the number samples
Applying those will get you out of the morass; if one of them doesn't hold you've made a mistake somewhere!

Sign in to comment.

More Answers (1)

dpb
dpb on 11 Aug 2022
Edited: dpb on 12 Aug 2022
I think it's absurd to even think about peforming spectral analysis on such a intermittently-sampled signal, but the only thing even close to a dominant frequency would be at roughly 0.015/hr and could only be distinguished at all after detrending the trace to remove DC component.
To believe this would be of any real meaning, however, would be nuts...
  5 Comments
dpb
dpb on 12 Aug 2022
Edited: dpb on 12 Aug 2022
The code to generate dT actually was more like
tD.T=tD.Date-tD.Date(1); % the time vector
tD.T.Format='h'; % set the format
tD.dT=[diff(tD.T);nan]; % must have height(tD) elements in table
Salt to suit...

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!