解决MySql布尔型新旧版本兼容问题,采用枚举来表示布尔型的数据表。由正向工程赋值
|
# 降采样与插值算法框架
## 概述
`NewLife.Algorithms` 命名空间提供了一套时序数据降采样和插值的算法框架,包含抽象接口和多种实现。适用于大量时序点的可视化展示、数据压缩和趋势分析场景。
**命名空间**:`NewLife.Algorithms`
**文档地址**:https://newlifex.com/core/sampling
## 核心接口
### ISampling — 采样接口
```csharp
public interface ISampling
{
/// <summary>对齐模式。每个桶 X 轴对齐方式</summary>
AlignModes AlignMode { get; set; }
/// <summary>插值填充算法</summary>
IInterpolation? Interpolation { get; set; }
/// <summary>降采样处理</summary>
TimePoint[] Down(TimePoint[] data, Int32 threshold);
/// <summary>混合处理,降采样和插值</summary>
TimePoint[] Process(TimePoint[] data, Int32 threshold, Int32 newCount);
}
```
### IInterpolation — 插值接口
```csharp
public interface IInterpolation
{
/// <summary>插值处理</summary>
Double Process(TimePoint[] data, Int32 prev, Int32 next, Int64 current);
}
```
### AlignModes — 对齐模式枚举
| 模式 | 说明 |
|------|------|
| `None` | 不对齐,取桶起始点时间(默认) |
| `Left` | 左对齐,使用桶起始数据点时间 |
| `Center` | 中间对齐,取桶内中间位置的时间 |
| `Right` | 右对齐,使用桶结束前一个数据点时间 |
## 内置实现
### 降采样
| 类 | 说明 |
|------|------|
| `AverageSampling` | 平均值降采样,每个桶内取平均值作为代表点 |
| `LinearInterpolation` | 线性插值填充,用于将降采样后的点还原到目标数量 |
### 插值
| 类 | 说明 |
|------|------|
| `LinearInterpolation` | 线性插值,公式:`v = v₀ + (t - t₀) * (v₁ - v₀) / (t₁ - t₀)` |
## 快速开始
```csharp
using NewLife.Algorithms;
// 创建降采样器
var sampler = new AverageSampling
{
AlignMode = AlignModes.Left,
};
// 原始时序数据(时间戳,数值)
var data = new[]
{
new TimePoint(1000, 10),
new TimePoint(2000, 20),
new TimePoint(3000, 30),
new TimePoint(4000, 40),
new TimePoint(5000, 50),
};
// 降采样到 3 个点
var result = sampler.Down(data, 3);
// 输出:[1000, 15], [3000, 35], [5000, 50]
```
## 使用场景
- **图表绘制**:将大量时序点降采样到屏幕像素宽度
- **数据压缩**:保留趋势特征的同时减少数据量
- **趋势分析**:通过降采样消除噪声,展示宏观走势
|