moving-average

How can I (efficiently) compute a moving average of a vector?

旧街凉风 提交于 2019-11-28 21:58:53
I've got a vector and I want to calculate the moving average of it (using a window of width 5). For instance, if the vector in question is [1,2,3,4,5,6,7,8] , then the first entry of the resulting vector should be the sum of all entries in [1,2,3,4,5] (i.e. 15 ); the second entry of the resulting vector should be the sum of all entries in [2,3,4,5,6] (i.e. 20 ); etc. In the end, the resulting vector should be [15,20,25,30] . How can I do that? jub0bs The conv function is right up your alley: >> x = 1:8; >> y = conv(x, ones(1,5), 'valid') y = 15 20 25 30 Benchmark Three answers, three different

How do you use a moving average to filter out accelerometer values in iPhone OS

点点圈 提交于 2019-11-28 17:20:21
I want to filter the accelerometer values using a moving average, how is this done? Thanks A simple, single pole, low pass, recursive IIR filter is quick and easy to implement, e.g. xf = k * xf + (1.0 - k) * x; yf = k * yf + (1.0 - k) * y; where x, y are the raw (unfiltered) X/Y accelerometer signals, xf, yf are the filtered output signals, and k determines the time constant of the filters (typically a value between 0.9 and 0.9999..., where a bigger k means a longer time constant). You can determine k empirically, or if you know your required cut-off frequency, Fc , then you can use the

Understanding NumPy's Convolve

谁都会走 提交于 2019-11-28 15:38:34
When calculating a simple moving average, numpy.convolve appears to do the job. Question: How is the calculation done when you use np.convolve(values, weights, 'valid') ? When the docs mentioned convolution product is only given for points where the signals overlap completely , what are the 2 signals referring to? If any explanations can include examples and illustrations, it will be extremely useful. window = 10 weights = np.repeat(1.0, window)/window smas = np.convolve(values, weights, 'valid') Convolution is a mathematical operator primarily used in signal processing. Numpy simply uses this

Grouped moving average in r

微笑、不失礼 提交于 2019-11-28 14:10:11
I'm trying to calculate a moving average in r over a particular field BUT I need this moving average to be grouped by two or more other fields. The purpose of this new average is for predictive analysis so I need it to be trailing as well. Any variables that do not have enough values to be averaged (such as student J) would ideally give either NA or its original Score value. I've been trying rollapply and data.table and am having no luck! I've provided the table of data and two moving averages (AVG2 with k=2 and AVG3 with k=3) to show exactly what I'm after. The moving average is on Score and

Computing a moving average

淺唱寂寞╮ 提交于 2019-11-28 11:40:20
I need to compute a moving average over a data series, within a for loop. I have to get the moving average over N=9 days. The array I'm computing in is 4 series of 365 values (M), which itself are mean values of another set of data. I want to plot the mean values of my data with the moving average in one plot. I googled a bit about moving averages and the "conv" command and found something which i tried implementing in my code.: hold on for ii=1:4; M=mean(C{ii},2) wts = [1/24;repmat(1/12,11,1);1/24]; Ms=conv(M,wts,'valid') plot(M) plot(Ms,'r') end hold off So basically, I compute my mean and

Replace NaN or missing values with rolling mean or other interpolation

末鹿安然 提交于 2019-11-28 10:09:43
I have a pandas dataframe with monthly data that I want to compute a 12 months moving average for. Data for for every month of January is missing, however (NaN), so I am using pd.rolling_mean(data["variable"]), 12, center=True) but it just gives me all NaN values. Is there a simple way that I can ignore the NaN values? I understand that in practice this would become a 11-month moving average. The dataframe has other variables which have January data, so I don't want to just throw out the January columns and do an 11 month moving average. There are several ways to approach this, and the best

applying rolling mean by group in R

南楼画角 提交于 2019-11-28 10:03:37
I'm an R newbie and I'm having a lot of trouble doing something that is probably very simple. I have a big dataset split up into groups by country code, and I want to take a 3-month rolling average of a price index, by country, and then put it into a new column that matches up to the appropriate month. I've been trying to use rollmean like this with no success (code and error messages below): > leader$last3<-tapply(leader, leader$ccode, function(x) rollmean(leader$GI_delta, 3, na.pad=T)) Error in tapply(leader, leader$ccode, function(x) rollmean(leader$GI_delta, : arguments must have same

Conditional rolling mean (moving average) on irregular time series

天涯浪子 提交于 2019-11-28 08:37:55
I have a group of data in the format: ID Minutes Value xxxx 118 3 xxxx 121 4 xxxx 122 3 yyyy 122 6 xxxx 123 4 yyyy 123 8 ... ... .... Each ID is a patient and each value is, say, blood pressure for that minute. I would like to create a rolling average for the 60 minutes before and 60 minutes after each point. However - as you can see, there are missing minutes (so I cannot merely use row numbers) and I would like to create average for each unique ID (so the average for ID xxxx cannot include values assigned to ID yyyy). It sounds like rollapply or rollingstat might be options, but have had

Calculate rolling / moving average in C++

試著忘記壹切 提交于 2019-11-28 03:28:12
I know this is achievable with boost as per: Using boost::accumulators, how can I reset a rolling window size, does it keep extra history? But I really would like to avoid using boost. I have googled and not found any suitable or readable examples. Basically I want to track the moving average of an ongoing stream of a stream of floating point numbers using the most recent 1000 numbers as a data sample. What is the easiest way to achieve this? I experimented with using a circular array, exponential moving average and a more simple moving average and found that the results from the circular

How to calculate moving average without keeping the count and data-total?

*爱你&永不变心* 提交于 2019-11-28 02:45:24
I am trying to find a way to calculate a moving cumulative average without storing the count and total data that is received so far. I came up with two algorithms but both need to store the count: new average = ((old count * old data) + next data) / next count new average = old average + (next data - old average) / next count The problem with these methods is that the count gets bigger and bigger resulting in losing precision in the resulting average. The first method uses the old count and next count which are obviously 1 apart. This got me thinking that perhaps there is a way to remove the