将一系列坐标值转换为具有均匀间隔的新系列。
为何使用 SwiftSeriesResampler?一个典型的用途是准备一组坐标,用于在图表中绘制,该图表要求沿 x 轴具有均匀间隔,例如在时间序列中。
可以作为一个开源 Swift 库使用,并可以集成到其他应用程序中。
SwiftSeriesResampler 是 OpenAlloc 开源 Swift 软件工具家族的一部分。
此示例展示了如何将时间序列中的市值数据从 5 个非均匀间隔重采样到目标中的 8 个均匀间隔,如图表所示。
let df = ISO8601DateFormatter()
let d1 = df.date(from: "2020-06-01T10:00:00Z")!
let d2 = df.date(from: "2020-06-20T08:00:00Z")!
let d3 = df.date(from: "2020-06-28T09:00:00Z")!
let d4 = df.date(from: "2020-07-18T11:00:00Z")!
let d5 = df.date(from: "2020-07-30T13:00:00Z")!
let intervals = [d1, d2, d3, d4, d5].map { $0.timeIntervalSinceReferenceDate }
let marketValues = [1300.0, 1600.0, 1200.0, 800.0, 1500.0]
let s = LerpResampler(intervals, targetCount: 8)!
let targetMVs = s.resample(marketValues)
for pair in zip(s.targetVals, targetMVs) {
let d = Date(timeIntervalSinceReferenceDate: pair.0)
print(String(format: "%@: $%5.0f", df.string(from: d), pair.1))
}
=>
"2020-06-01T10:00:00Z: $ 1300"
"2020-06-09T20:42:51Z: $ 1434"
"2020-06-18T07:25:42Z: $ 1568"
"2020-06-26T18:08:34Z: $ 1281"
"2020-07-05T04:51:25Z: $ 1064"
"2020-07-13T15:34:17Z: $ 896"
"2020-07-22T02:17:08Z: $ 1011"
"2020-07-30T13:00:00Z: $ 1500"
AccelLerpResamplerD
的代码未显示,但可以通过将 LerpResampler
替换为它来轻松复制。
目前提供两种重采样器,均基于线性插值技术。
如上图所示,两种重采样器的行为不同,因为它们处理插值的方式不同。
一种基本的重采样器,采用纯 Swift 线性插值器(又名 lerp)。
实际上是两种重采样器,它们采用 Apple 的 Accelerate 框架中的线性插值器。
单精度版本 AccelLerpResamplerS
用于 Float
和类似数据类型。
双精度版本 AccelLerpResamplerD
用于 Double
和类似数据类型。特别是 TimeInterval
,它对于绘制时间序列非常有用。
所有重采样器都派生自 BaseResampler
类,该类提供以下公共属性和方法。
派生的重采样器可能提供额外的公共属性和方法。
init?(_ xVals: [T], targetCount: Int)
- 创建一个新的重采样器实例其中 T
是你的 BinaryFloatingPoint
数据类型。 请注意,某些重采样器具有固定的类型。
初始化是有条件的,如果参数是无意义的,则返回 nil
,例如,如果 xVals
不是升序排列。
初始化值也可以作为属性使用
let xVals: [T]
- 沿 x 轴的原始坐标。它们不需要是均匀间隔的。
let targetCount: Int
- 沿 x 轴均匀间隔的目标坐标数,用于重采样。
计算属性是惰性的,这意味着它们仅在首次需要时才进行计算。
var horizontalExtent: T
- 分隔第一个和最后一个 xVal
的距离。
var relativeDistances: [T]
- 从第一个 xVal
开始的 xVal
距离。 第一个是 0
。 最后一个等于 horizontalExtent
。
var targetInterval: T
- 重采样坐标之间每个间隔的宽度。
var targetStride: [T]
- 重采样的 x 轴坐标,作为 stride 对象。 它们是均匀间隔的。
var targetVals: [T]
- 重采样的 x 轴坐标,作为一个数组对象。 它们是均匀间隔的。
func resample(yVals: [T]) -> [T]
- 重采样指定的 y 坐标值数组,该数组对应于初始化重采样器时提供的原始 xVals
。 返回 targetCount
个重采样的 y 坐标值。This library is a member of the OpenAlloc Project(此库是 OpenAlloc 项目 的成员)。
Copyright 2021, 2022 OpenAlloc LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at(根据 Apache 许可证 2.0 版(“许可证”)获得许可;除非遵守许可证,否则您不得使用此文件。 您可以在以下位置获取许可证副本:)
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.(除非适用法律要求或以书面形式达成协议,否则本软件是按“原样”基础分发的,不作任何形式的担保或条件,无论是明示的还是暗示的。 有关许可的具体语言管理权限和限制,请参阅许可证。)
Other contributions are welcome too. You are encouraged to submit pull requests to fix bugs, improve documentation, or offer new features.(也欢迎其他贡献。 鼓励您提交拉取请求以修复错误、改进文档或提供新功能。)
The pull request need not be a production-ready feature or fix. It can be a draft of proposed changes, or simply a test to show that expected behavior is buggy. Discussion on the pull request can proceed from there.(拉取请求不必是生产就绪的功能或修复。 它可以是提议更改的草案,或者只是一个测试,表明预期行为存在错误。 关于拉取请求的讨论可以从那里开始。)
Contributions should ultimately have adequate test coverage. See tests for current entities to see what coverage is expected.(贡献最终应具有足够的测试覆盖率。 请参阅当前实体的测试,以了解预期的覆盖率。)