سؤال

I have hourly data and I want to do find the daily max 8-hour average. Basically, for each hour of the day, I want to do an 8-hour average. So take the average of 0:00 to 8:00, then 1:00 to 9:00, etc.), so I end up with 24 8-hour average periods (with some running into the next day of course). Then I need to take the maximum of those 24 8-hour averages to get the daily max. 

The .mat file used can be found here: https://www.dropbox.com/sh/9e2dgm0imvr0hpe/tAUOtpZEEa

A note about the format of the file: The O3.mat file has a variable called O3_Sorted that is a cell array. It contains all data, sorted already. But the data contains information from more than one site (i.e. there is information from different places). The information for each site is sorted together, but in the code, when I try to find the 8-hour averages, I have to pull out one site at a time so that the averages don't run into the beginning of the data for another place.

Here's a sample of what things look like. I included one day for one site and half a day of another site. The actual file has a month of data for each of these sites and other sites as well. As you can see, sometimes, the data is missing. Column 1 - Site name Column 2 - Date Column 3 - Hour Column 4 - Data

003-0010    2007-05-31  00:00   0.016
003-0010    2007-05-31  01:00   0.015
003-0010    2007-05-31  02:00   0.002
003-0010    2007-05-31  03:00   0.03
003-0010    2007-05-31  04:00   0.019
003-0010    2007-05-31  05:00   0.013
003-0010    2007-05-31  06:00   0.018
003-0010    2007-05-31  07:00   0.024
003-0010    2007-05-31  08:00   0.031
003-0010    2007-05-31  09:00   0.029
003-0010    2007-05-31  10:00   0.031
003-0010    2007-05-31  11:00   0.035
003-0010    2007-05-31  12:00   0.026
003-0010    2007-05-31  13:00   0.026
003-0010    2007-05-31  14:00   0.033
003-0010    2007-05-31  15:00   0.039
003-0010    2007-05-31  16:00   0.036
003-0010    2007-05-31  17:00   0.035
003-0010    2007-05-31  18:00   0.031
003-0010    2007-05-31  19:00   0.03
003-0010    2007-05-31  20:00   0.03
003-0010    2007-05-31  21:00   0.017
003-0010    2007-05-31  22:00   0.017
003-0010    2007-05-31  23:00   0.007
027-0007    2007-05-31  00:00   0.045
027-0007    2007-05-31  01:00   0.043
027-0007    2007-05-31  02:00   
027-0007    2007-05-31  03:00   0.038
027-0007    2007-05-31  04:00   0.037
027-0007    2007-05-31  05:00   0.034
027-0007    2007-05-31  06:00   0.034
027-0007    2007-05-31  07:00   0.038
027-0007    2007-05-31  08:00   0.044
027-0007    2007-05-31  09:00   0.05
027-0007    2007-05-31  10:00   0.054
027-0007    2007-05-31  11:00   0.051
027-0007    2007-05-31  12:00   0.047

Here is what I have so far:

for i = 1:size(O3_sites)
    I = ismember(D(:,6), O3_sites(i)); % Rows were the cell array O3_sorted has data corresponding to a certain site
    site = D(I,:);

    %% Convert O3 from ppm to ppb, 1ppm = 1000ppb
    x = 1000;
    y = str2double(O3);
    O3_data = bsxfun(@times,x,y); % ppb    

% Find size of array
[M, N]= size(O3_data); 

% Create empty array
O3_MD8 = zeros(N,M-7); % double

**% Do a loop to calculate the running mean
for j = 1:M-7
    A = O3_data(j:j+7);
    O3_MD8(:,j) = mean(A);
end**

% Find max from each 8-hour loop

end

After I get the 8-hour averages, how can I ask MATLAB to find the max for each 24 averages? Basically, get the max of the hourly averages.

Also, the method I'm trying to do now is a bit risky because I'm not using datenum and so if data is missing a day, I won't know. But I have no idea how to consider that when writing the code.

هل كانت مفيدة؟

المحلول

You could just use the filter function, though I assume you already got your data in a proper format (1D-vector)

hours = 8;   % size of hour window defining the moving average
movAV = filter(ones(1,hours)/hours,1,O3_data);

For the daily maximum you need to split your "hour"-vector and movAV in 24h brackets. Assuming you have one value per hour you could just reshape your result into a 24 x N array:

%example
x = 1:240;   %d ata for 10 days
y = reshape(x,24,[])

then use the additional parameters of the max function to search the max columnwise:

% in this case the max is always the last value of every day
dailyMax = max(y,[],1) 

dailyMax =

    24    48    72    96   120   144   168   192   216   240

respectively:

dailyMax = max(reshape(movAV,24,[]),[],1)

Probably for your case the most convenient would be to use findpeaks which would directly output all local maxima (Signal Processing Toolbox required).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top