user2895146
user2895146

Reputation: 55

Matlab: time series plot for 44100 hz data

I'm trying to plot a time series which has been collected at a rate of 44100 hz. I would like to have the time (in seconds) and possibly the date on the x-axis.

Say I have data for one minute, i.e. 2646001 data points and assume, for simplicity, all data points are ones:

y=repmat(1,2646001,1);

I created a vector of date numbers by converting the start and end date into serial date numbers and then create a vector from the first time number to the last time number with rate 44100 hz:

StartTimeNum    = datenum(2013,11,12,23,00,0);
EndTimeNum      = datenum(2013,11,12,23,01,0);

T               = EndTimeNum-StartTimeNum;

TimeNum         = StartTimeNum:(T/length(y)):EndTimeNum;

I then define the format I would like the date string to be in and convert the time number vector into time string.

FormatOut       = 'dd/mm/yy, HH:MM:SS.FFF';

TimeStr= datestr(TimeNum, FormatOut);

but now TimeStr is a <2646001x22 char>, rather than a <2646001x1 char> which Matlab doesn't allow me to use as the input for the x-axis.

In another attempt I found the timeseries class (http://www.mathworks.co.uk/help/matlab/ref/timeseries.plot.html) which would be perfect, but because my data is in 44100 hz, I am not sure how to define the units (ts1.TimeInfo.Units), which are generally described as 'days', or 'hours' or 'seconds' but not in hz...

Is there any way around this?

Thanks

Upvotes: 2

Views: 348

Answers (2)

lennon310
lennon310

Reputation: 12689

y=ones(2646001,1);  % use ones(m,n) for more efficiency

StartTimeNum    = datenum(2013,11,12,23,00,0);
EndTimeNum      = datenum(2013,11,12,23,01,0);

T               = EndTimeNum-StartTimeNum;

TimeNum         = StartTimeNum:(T/(length(y)-1)):EndTimeNum;  % length consistent
FormatOut       = 'dd/mm/yy, HH:MM:SS.FFF';
figure,plot(TimeNum, y),datetick('x',FormatOut)

Upvotes: 3

Peter
Peter

Reputation: 14927

Plot your data vs. TimeNum directly, then use datetick to set the labels:

plot(TimeNum, y);
datetick('x', 'dd/mm/yy, HH:MM:SS.FFF');

Or try just datetick with no arguments. The default format might be better.

Upvotes: 1

Related Questions