series_downsample_fl()series_downsample_fl()

函数 series_downsample_fl() 按整数因子对时序减少采样The function series_downsample_fl() downsamples a time series by an integer factor. 此函数采用包含多个时序(动态数值阵列)的表,并对每个序列减少采样。This function takes a table containing multiple time series (dynamic numerical array), and downsamples each series. 输出包含较粗的序列及其各自的时间数组。The output contains both the coarser series and its respective times array. 为了避免别名,该函数会在子采样之前对每个序列都应用简单的低通筛选器To avoid aliasing, the function applies a simple low pass filter on each series before subsampling.

备注

此函数是 UDF(用户定义的函数)This function is a UDF (user-defined function). 有关详细信息,请参阅用法For more information, see usage.

语法Syntax

T | invoke series_downsample_fl(t_col, y_col, ds_t_col, ds_y_col, sampling_factor)T | invoke series_downsample_fl(t_col, y_col, ds_t_col, ds_y_col, sampling_factor)

参数Arguments

  • t_col:输出表的列(其中包含要减少采样的序列的时间轴)的名称。t_col: The name of the column (of the input table) containing the time axis of the series to downsample.
  • y_col:输出表的列(其中包含要减少采样的序列)的名称。y_col: The name of the column (of the input table) containing the series to downsample.
  • ds_t_col:列(用于存储每个序列的已减少采样的时间轴)的名称。ds_t_col: The name of the column to store the downsampled time axis of each series.
  • ds_y_col:列(用于存储已减少采样的序列)的名称。ds_y_col: The name of the column to store the down sampled series.
  • sampling_factor:一个整数,指定所需要的减少采样程度。sampling_factor: an integer specifying the required down sampling.

使用情况Usage

series_downsample_fl() 是用户定义的表格函数,需使用 invoke 运算符进行应用。series_downsample_fl() is a user-defined tabular function, to be applied using the invoke operator. 可以在查询中嵌入其代码,或将其安装在数据库中。You can either embed its code in your query, or install it in your database. 用法选项有两种:临时使用和永久使用。There are two usage options: ad hoc and persistent usage. 有关示例,请参阅下面的选项卡。See the below tabs for examples.

如果是临时使用,请使用 let 语句嵌入该函数的代码。For ad hoc usage, embed its code using let statement. 不需要权限。No permission is required.

let series_downsample_fl=(tbl:(*), t_col:string, y_col:string, ds_t_col:string, ds_y_col:string, sampling_factor:int)
{
    tbl
    | extend _t_ = column_ifexists(t_col, dynamic(0)), _y_ = column_ifexists(y_col, dynamic(0))
    | extend _y_ = series_fir(_y_, repeat(1, sampling_factor), true, true)    //  apply a simple low pass filter before sub-sampling
    | mv-apply _t_ to typeof(DateTime), _y_ to typeof(double) on
    (extend rid=row_number()-1
    | where rid % sampling_factor == ceiling(sampling_factor/2.0)-1                    //  sub-sampling
    | summarize _t_ = make_list(_t_), _y_ = make_list(_y_))
    | extend cols = pack(ds_t_col, _t_, ds_y_col, _y_)
    | project-away _t_, _y_
    | evaluate bag_unpack(cols)
}
;
demo_make_series1
| make-series num=count() on TimeStamp step 1h by OsVer
| invoke series_downsample_fl('TimeStamp', 'num', 'coarse_TimeStamp', 'coarse_num', 4)
| render timechart with(xcolumn=coarse_TimeStamp, ycolumns=coarse_num)

该时序减少采样的程度为 4:此图显示时序减少采样

下面是原始时序(在减少采样之前),供参考:For reference, here is the original time series (before downsampling):

demo_make_series1
| make-series num=count() on TimeStamp step 1h by OsVer
| render timechart with(xcolumn=TimeStamp, ycolumns=num)

此图显示在减少采样之前的原始时序