matlab - averaging for a given interval - matlab
I can calculate daily averages of a data set like so:
Jday = datenum('2010-11-01 00:00','yyyy-mm-dd HH:MM'):60/(60*24):...
datenum('2011-02-31 23:00','yyyy-mm-dd HH:MM');
Dat = rand(length(Jday),1);
DateV = datevec(Jday);
[~,~,b] = unique(DateV(:,1:3),'rows');
AvDat = abs(accumarray(b,Dat,[],#nanmean));
AvJday = abs(accumarray(b,Jday,[],#nanmean));
However, I would like to take an average of a data set given a number for the output resolution. For example, if I wrote
outRes = 86400; % in seconds
I would like to average the values so that the output resolution is equal to 86400 seconds, and if the outRes defined is shorter than the resolution of the data then no averaging will take place.
How can this be done?
You should be able to detect the resolution of the data by:
CurrentRes=mean(diff(Jday))*86400;
if (~all(diff(Jday)==CurrentRes))
error('Inequally distributed sampling times');
end
if (CurrentRes>outRes)
return;
Then the amount you need to downsample by is:
AveragingFactor=outRes/CurrentRes;
Then you can average by discarding some samples at the end (or start) and averaging:
Dat = reshape(Dat(1:end-mod(length(Dat),AveragingFactor),:),AveragingFactor,[]);
AvDat = mean(Dat,1)'; % Transpose to keep it as a column vector if necessary.
Related
How to increase time by 1s and replace old DT_i with the new one
I'm trying to calculate the DT value from a model I set up on Sim4Life. Firstly, i'd like to say that I am a complete beginner and I am trying to understand how programming works in general. Now, I have a function with some constants and two variables, the one being time Dt (starting from 1 sec to 900 secs) and the other being the initial DT_i value. I want to calculate the increase of temperature for every second and create a loop that replaces the DT_i value with the DT_1_i value and also calculates the increased temperature DT_i_1. The function looks like this: DT_1_i=DT_i+Dt. I know it is a very simple problem but I couldn't work my way through other similar questions. Any help would be appreciated.
Temperature variation: You need initial temperature variation , I used 0 T(i+1) stands for Next temperature variation T(i) stands for present temperature variation i stands for time step, dt Read through comment in my code Time Use for loop to set the time for i = 1 : 900 %Temperature increase end i =1:900 just means first run use time = 1s, second run time = 1+1 = 2 so on till 900 The code is as follow % Initial Temperature variation is set to zero, unless you have some data d = 1.3; c = 3.7; S_i = 3*10^3; t_reg = 900; %Time t = 1:900; % Length oftime to help me know the size of the variable needed to % initialize l = length(t); % Initialize variable that used to store DT it helps speed up % comutation % Initial Temperature variation is set to zero, unless you have some data DT = zeros(1, l); for i = 1:900 % the value of i represent dt, first run i = 1, dt = 1, second run % i = 2 and dt = 2 so on if i == 900 %do nothing already reached the last index 900, i+1 = 901 will be %out of range else DT(i+1) = DT(i) + (i./t_reg).*(d.*sqrt(c*S_i)-DT(i+1)); end end
Getting the correct output units from the PLOMB (Lomb-scargle periodogram) function
I am trying to analyze timeseries of wheel turns that were sampled at 1 minute intervals for 10 days. t is a 1 x 14000 array that goes from .1666 hours to 240 hours. analysis.timeseries.(grp).(chs) is a 1 x 14000 array for each of my groups of interest and their specific channels that specifize activity at each minute sampled. I'm interested in collecting the maximum power and the frequency it occurs at. My problem is I'm not sure what units f is coming out in. I would like to have it return in cycles per hour and span to a maximum period of 30 hours. I tried to use the Galileo example in the documentation as a guide, but it didn't seem to work. Below is my code: groups = {'GFF' 'GMF' 'SFF' 'SMF'}; chgroups = {chnamesGF chnamesGM chnamesSF chnamesSM}; t1 = (t * 3600); %matlab treats this as seconds so convert it to an hour form onehour = seconds(hours(1)); for i = 1:4 grp = groups{1,i}; chn = chgroups{1,i}; for channel = 1:length(chn) chs = chn{channel,1}; [pxx,f]= plomb(analysis.timeseries.(grp).(chs),t, 30/onehour,'normalized'); analysis.pxx.(grp).(chs) = pxx; analysis.f.(grp).(chs) = f; analysis.lsp.power.(grp).(chs) = max(pxx); [row,col,v] = find(analysis.pxx.(grp).(chs) == analysis.lsp.power.(grp).(chs)); analysis.lsp.tau.(grp).(chs) = analysis.f.(grp).(chs)(row); end end
Not really an answer but it is hard to put a image in a comment. Judging by this (plomb manual matlab), I think that pxx is without dimension as for f is is the frequency so 1/(dimension of t) dimension. If your t is in hours I would say h^-1. So I'd rather say try [pxx,f]= plomb(analysis.timeseries.(grp).(chs),t*30.0/onehour,'normalized');
What's wrong with my code in Matlab?
I want to play all frequencies given in matrix(FrTm) with its duration.The actual duration is one second but each frequency has to play for 3 of 1/18 and 6 of 1/18 seocnd such as given in matrix(FrTm). function Music() Fs=44100; T=1/Fs; M = zeros(1,88); for I=7:88, M(I) = round(36.8*(2^(1/12))^(I-6)); end Signal=[]; FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6; 49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6; 50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6; 45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6; 50,6]; t=0:1/18:1; for i=1:length(FrTm), M(i)=FrTm(i); Z=M(i); data= sin(2*pi*Z/Fs*t); signal=[data;signal]; end stem(Signal); sound (Signal, 44100); end
The classical way to make a sound with a given frequency (f) and sample frequency (Fs) is to make a time vector with step 1/Fs: time = 0:1/Fs:D; Where Dis the duration of the signal. The signal itself is then: signal = sin(2*pi*f.*time) In this case the total time is fixed, not the time of each signal. The total time is denoted with T, and the total time vector is made as time = 0:1/Fs:T; The sum of the second column is the total number of units the vector time needs to be divided in, e.g. 50, 3 means that a signal at 50 Hz needs to be played for 3 units. This means we only need a time vector of the length of 3 units: t = time(1:floor(end*duration/s)); Where duration is the number of units for this part and s is the total number of units. The signal is then simply, as stated above, data = sin(2*pi*f*t); The data is then appended to the whole signal. The complete code, looks like this: Fs = 44100; % sample frequency [Hz] T = 3; % total duration [s] time = 0:1/Fs:T; % matrix with frequencies and duration FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6; 49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6; 50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6; 45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6; 50,6]; s = sum(FrTm(:,2)); [m, ~] = size(FrTm); signal = []; for i=1:m freq = FrTm(i,1); duration = FrTm(i,2); t = time(1:floor(end*duration/s)); data = 10*sin(2*pi*freq.*t); signal = [data signal]; end stem(signal); sound(signal, 44100); Note instead of declaring time in the beginning, it is possible to make a new vector each time you run through the loop. In that case omit time = 0:1/Fs:T; and change t = time(1:floor(end*duration/s)); to t = 0:1/Fs:floor(end*duration/s);
function Music() Fs=44100; T=1/Fs; M = zeros(1,88); for I=7:88, M(I) = round(36.8*(2^(1/12))^(I-6)); end Signal=[]; FrTm=[50 3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6; 49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6; 50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6; 45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6; 50,6]; for i=1:length(FrTm), %--------------------------------------------------- % complete the function freq = FrTm(i,1); duration = FrTm(i,2); time =0:1/Fs:1; % change the 1 to change total duration s = sum(FrTm(:,2)); t = time(1:floor(end*duration/s)); data = sin(2*pi*freq.*t); Signal = [data Signal]; end stem(Signal); sound (Signal, 44100); end
This is the exact code what i wanted ViG can you please remove this tak tak sound it just a noise actually how to use envelope function to remove thid tak tak sound in music code is following. Fs=44100; T=1/Fs; M=zeros(1,88); for I=7:88 M(I)=round(36.8*(2^(1/12))^(I-6)); end signal=[]; FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6; 49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6; 50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6; 45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6; 50,6]; for i=1:length(FrTm) x=FrTm(i,1); y=FrTm(i,2); F=M(x); time=0:1/Fs:y/18; sinewave=sin(2*pi*F*time); signal=[signal sinewave]; end stem(signal) sound(signal,Fs)
Count time intervals for events greater than threshold
I am stuck at something in MATLAB and would appreciate if someone can help. Here is what I would like to do. I have a velocity field data w.r.t. time (see figure attached). I would like to count number of time intervals (T1,T2,T3,etc) for which value of velocity is greater than threshold. Please note that I would like to count time intervals and not number of velocity points above threshold. After doing step 1, I would like to somehow calculate average of time duration (e.g. average of T1, T2, T3, etc). Finally, I want to calculate average value of velocity data for intervals T1, T2, T3, etc. I think step 2 and 3 should be straightforward if I can get step 1 done. I would really appreciate any help. I am doing this in MATLAB but I am open to use any other tools if required.
I'd suggest the use of bwconncomp: A = rand(1,100); %your 1d list of speeds T = 0.5; B = A > T; tmp = bwconncomp(B); pxlist = tmp.PixelIdxList; %connected components are above thresh N = zeros(1,length(pxlist)); V = zeros(1,length(pxlist)); for li = 1:length(pxlist) N(li) = length(pxlist{li}); V(li) = mean(A(pxlist{li})); %avg velocity in interval li end avg = mean(N); %this is the avg. "time", i.e. length of connected components
analyzing time series in the frequency domain
I am attempting to analyse a time series with spectral analysis. I am trying to detect any periodicities in my data, which is composed of hourly measurements recorded for one week (24 * 7 = 168 measurements), I aim to show the diurnal component of the temperature variation. So far I have (for example): clear all StartDate = '2011-07-01 00:00'; EndDate = '2011-07-07 23:00'; DateTime=datestr(datenum(StartDate,'yyyy-mm-dd HH:MM'):60/(60*24):... datenum(EndDate,'yyyy-mm-dd HH:MM'),... 'yyyy-mm-dd HH:MM'); DateTime=cellstr(DateTime); DecDay = datenum(DateTime)-datenum(2011,0,0); t = 0:25/length(DecDay):(25-0.1488); x = sin(2*pi*50*t) + sin(2*pi*120*t); y = x + 2*randn(size(t)); Y = fft(y,length(y)); Where would I go from here? any advice would be much appreciated. Altered: clear all StartDate = '2011-07-01 00:00'; EndDate = '2011-07-07 23:00'; DateTime=datestr(datenum(StartDate,'yyyy-mm-dd HH:MM'):60/(60*24):... datenum(EndDate,'yyyy-mm-dd HH:MM'),... 'yyyy-mm-dd HH:MM'); DateTime=cellstr(DateTime); DecDay = datenum(DateTime)-datenum(2011,0,0); x = cos((2*pi)/12*DecDay)+randn(size(DecDay)); % if you have the signal processing toolbox [Pxx,F] = periodogram(x,rectwin(length(x)),length(x),1); plot(F,10*log10(Pxx)); xlabel('Cycles/hour'); ylabel('dB/(Cycles/hour'); Can anyone suggest how I would convert the x axis to hours instead of cycles per hour? I have tried plot(1./F,10*log10(Pxx)); xlabel('hours'); but this messes up the peridogram.
You may find it easier to start off with MATLAB's periodogram function, rather than trying to use the FFT directly. This takes care of windowing the data for you and various other implementation details.