Applies a convolution filter to a univariate time series.
Arguments
- x
A
column vector
of length T- filter
A
column vector
of length f- sides
An
int
that takes either 1:for using past values only or 2: filter coefficients are centered around lag 0.- circular
A
bool
that indicates if the filter should be wrapped around the ends of the time series.
Details
This is a port of the cfilter function harnessed by the filter function in stats. It is about 5-7 times faster than R's base function. The benchmark was done on iMac Late 2013 using vecLib as the BLAS.
Examples
x = 1:100
#
cfilter(x, rep(1, 3), sides = 2, circular = FALSE)
#> [,1]
#> [1,] NA
#> [2,] 6
#> [3,] 9
#> [4,] 12
#> [5,] 15
#> [6,] 18
#> [7,] 21
#> [8,] 24
#> [9,] 27
#> [10,] 30
#> [11,] 33
#> [12,] 36
#> [13,] 39
#> [14,] 42
#> [15,] 45
#> [16,] 48
#> [17,] 51
#> [18,] 54
#> [19,] 57
#> [20,] 60
#> [21,] 63
#> [22,] 66
#> [23,] 69
#> [24,] 72
#> [25,] 75
#> [26,] 78
#> [27,] 81
#> [28,] 84
#> [29,] 87
#> [30,] 90
#> [31,] 93
#> [32,] 96
#> [33,] 99
#> [34,] 102
#> [35,] 105
#> [36,] 108
#> [37,] 111
#> [38,] 114
#> [39,] 117
#> [40,] 120
#> [41,] 123
#> [42,] 126
#> [43,] 129
#> [44,] 132
#> [45,] 135
#> [46,] 138
#> [47,] 141
#> [48,] 144
#> [49,] 147
#> [50,] 150
#> [51,] 153
#> [52,] 156
#> [53,] 159
#> [54,] 162
#> [55,] 165
#> [56,] 168
#> [57,] 171
#> [58,] 174
#> [59,] 177
#> [60,] 180
#> [61,] 183
#> [62,] 186
#> [63,] 189
#> [64,] 192
#> [65,] 195
#> [66,] 198
#> [67,] 201
#> [68,] 204
#> [69,] 207
#> [70,] 210
#> [71,] 213
#> [72,] 216
#> [73,] 219
#> [74,] 222
#> [75,] 225
#> [76,] 228
#> [77,] 231
#> [78,] 234
#> [79,] 237
#> [80,] 240
#> [81,] 243
#> [82,] 246
#> [83,] 249
#> [84,] 252
#> [85,] 255
#> [86,] 258
#> [87,] 261
#> [88,] 264
#> [89,] 267
#> [90,] 270
#> [91,] 273
#> [92,] 276
#> [93,] 279
#> [94,] 282
#> [95,] 285
#> [96,] 288
#> [97,] 291
#> [98,] 294
#> [99,] 297
#> [100,] NA
# Using R's function
filter(x, rep(1, 3))
#> Time Series:
#> Start = 1
#> End = 100
#> Frequency = 1
#> [1] NA 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54
#> [19] 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108
#> [37] 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162
#> [55] 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216
#> [73] 219 222 225 228 231 234 237 240 243 246 249 252 255 258 261 264 267 270
#> [91] 273 276 279 282 285 288 291 294 297 NA
#
cfilter(x, rep(1, 3), sides = 1, circular = FALSE)
#> [,1]
#> [1,] NA
#> [2,] NA
#> [3,] 6
#> [4,] 9
#> [5,] 12
#> [6,] 15
#> [7,] 18
#> [8,] 21
#> [9,] 24
#> [10,] 27
#> [11,] 30
#> [12,] 33
#> [13,] 36
#> [14,] 39
#> [15,] 42
#> [16,] 45
#> [17,] 48
#> [18,] 51
#> [19,] 54
#> [20,] 57
#> [21,] 60
#> [22,] 63
#> [23,] 66
#> [24,] 69
#> [25,] 72
#> [26,] 75
#> [27,] 78
#> [28,] 81
#> [29,] 84
#> [30,] 87
#> [31,] 90
#> [32,] 93
#> [33,] 96
#> [34,] 99
#> [35,] 102
#> [36,] 105
#> [37,] 108
#> [38,] 111
#> [39,] 114
#> [40,] 117
#> [41,] 120
#> [42,] 123
#> [43,] 126
#> [44,] 129
#> [45,] 132
#> [46,] 135
#> [47,] 138
#> [48,] 141
#> [49,] 144
#> [50,] 147
#> [51,] 150
#> [52,] 153
#> [53,] 156
#> [54,] 159
#> [55,] 162
#> [56,] 165
#> [57,] 168
#> [58,] 171
#> [59,] 174
#> [60,] 177
#> [61,] 180
#> [62,] 183
#> [63,] 186
#> [64,] 189
#> [65,] 192
#> [66,] 195
#> [67,] 198
#> [68,] 201
#> [69,] 204
#> [70,] 207
#> [71,] 210
#> [72,] 213
#> [73,] 216
#> [74,] 219
#> [75,] 222
#> [76,] 225
#> [77,] 228
#> [78,] 231
#> [79,] 234
#> [80,] 237
#> [81,] 240
#> [82,] 243
#> [83,] 246
#> [84,] 249
#> [85,] 252
#> [86,] 255
#> [87,] 258
#> [88,] 261
#> [89,] 264
#> [90,] 267
#> [91,] 270
#> [92,] 273
#> [93,] 276
#> [94,] 279
#> [95,] 282
#> [96,] 285
#> [97,] 288
#> [98,] 291
#> [99,] 294
#> [100,] 297
# Using R's function
filter(x, rep(1, 3), sides = 1)
#> Time Series:
#> Start = 1
#> End = 100
#> Frequency = 1
#> [1] NA NA 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51
#> [19] 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105
#> [37] 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159
#> [55] 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213
#> [73] 216 219 222 225 228 231 234 237 240 243 246 249 252 255 258 261 264 267
#> [91] 270 273 276 279 282 285 288 291 294 297
#
cfilter(x, rep(1, 3), sides = 1, circular = TRUE)
#> [,1]
#> [1,] 200
#> [2,] 103
#> [3,] 6
#> [4,] 9
#> [5,] 12
#> [6,] 15
#> [7,] 18
#> [8,] 21
#> [9,] 24
#> [10,] 27
#> [11,] 30
#> [12,] 33
#> [13,] 36
#> [14,] 39
#> [15,] 42
#> [16,] 45
#> [17,] 48
#> [18,] 51
#> [19,] 54
#> [20,] 57
#> [21,] 60
#> [22,] 63
#> [23,] 66
#> [24,] 69
#> [25,] 72
#> [26,] 75
#> [27,] 78
#> [28,] 81
#> [29,] 84
#> [30,] 87
#> [31,] 90
#> [32,] 93
#> [33,] 96
#> [34,] 99
#> [35,] 102
#> [36,] 105
#> [37,] 108
#> [38,] 111
#> [39,] 114
#> [40,] 117
#> [41,] 120
#> [42,] 123
#> [43,] 126
#> [44,] 129
#> [45,] 132
#> [46,] 135
#> [47,] 138
#> [48,] 141
#> [49,] 144
#> [50,] 147
#> [51,] 150
#> [52,] 153
#> [53,] 156
#> [54,] 159
#> [55,] 162
#> [56,] 165
#> [57,] 168
#> [58,] 171
#> [59,] 174
#> [60,] 177
#> [61,] 180
#> [62,] 183
#> [63,] 186
#> [64,] 189
#> [65,] 192
#> [66,] 195
#> [67,] 198
#> [68,] 201
#> [69,] 204
#> [70,] 207
#> [71,] 210
#> [72,] 213
#> [73,] 216
#> [74,] 219
#> [75,] 222
#> [76,] 225
#> [77,] 228
#> [78,] 231
#> [79,] 234
#> [80,] 237
#> [81,] 240
#> [82,] 243
#> [83,] 246
#> [84,] 249
#> [85,] 252
#> [86,] 255
#> [87,] 258
#> [88,] 261
#> [89,] 264
#> [90,] 267
#> [91,] 270
#> [92,] 273
#> [93,] 276
#> [94,] 279
#> [95,] 282
#> [96,] 285
#> [97,] 288
#> [98,] 291
#> [99,] 294
#> [100,] 297
# Using R's function
filter(x, rep(1, 3), sides = 1, circular = TRUE)
#> Time Series:
#> Start = 1
#> End = 100
#> Frequency = 1
#> [1] 200 103 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51
#> [19] 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105
#> [37] 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159
#> [55] 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213
#> [73] 216 219 222 225 228 231 234 237 240 243 246 249 252 255 258 261 264 267
#> [91] 270 273 276 279 282 285 288 291 294 297