/ concept-collection / timeseries-compressibility
Sign in
concept-collection / timeseries-compressibility
timeseries-compressibility / src / worker / compressWorker.ts
70 lines · 1.8 KBBlameHistoryRaw
1/**
2 * Compression measurements off the main thread, so the scrolling view never
3 * stutters while zstd -19 or the LPC fit runs. One message in (the model),
4 * one message out (the nine codec results).
5 */
6import { LatentSource } from '../model/latent'
7import {
8 initCodecs,
9 compressAll,
10 entropyBounds,
11 type BoundResult,
12 ZLIB,
13 ZSTD,
14 ANS,
15 DELTA_ZLIB,
16 DELTA_ZSTD,
17 DELTA_ANS,
18 LPC_ZLIB,
19 LPC_ZSTD,
20 LPC_ANS,
21 type CodecResult,
22} from '../compress/codecs'
24export interface CompressRequest {
25 id: number
26 kernel: Float64Array
27 sigma: number
28 dither: boolean
29 blockSize: number
30 seed: number
33export interface CompressResponse {
34 id: number
35 results: CodecResult[]
36 /** Order-0 entropy limit for each prefilter group. */
37 bounds: BoundResult[]
38 /** Empirical std of the quantized block, for display sanity. */
39 empiricalStd: number
40 error?: string
43const CODECS = [ZLIB, ZSTD, ANS, DELTA_ZLIB, DELTA_ZSTD, DELTA_ANS, LPC_ZLIB, LPC_ZSTD, LPC_ANS]
45const post = self.postMessage as (message: CompressResponse) => void
47self.onmessage = async (e: MessageEvent<CompressRequest>) => {
48 const { id, kernel, sigma, dither, blockSize, seed } = e.data
49 try {
50 await initCodecs()
51 const samples = new LatentSource(seed).window(0, blockSize, kernel, sigma, dither)
52 let sum = 0
53 let sumSq = 0
54 for (let i = 0; i < samples.length; i++) {
55 sum += samples[i]
56 sumSq += samples[i] * samples[i]
57 }
58 const mean = sum / samples.length
59 const empiricalStd = Math.sqrt(Math.max(0, sumSq / samples.length - mean * mean))
60 const bytes = new Uint8Array(samples.buffer, 0, samples.byteLength)
61 post({
62 id,
63 results: compressAll(bytes, CODECS),
64 bounds: entropyBounds(samples),
65 empiricalStd,
66 })
67 } catch (err) {
68 post({ id, results: [], bounds: [], empiricalStd: 0, error: String(err) })
69 }
moveopenescclose