/ concept-collection / timeseries-compressibility
Sign in
concept-collection / timeseries-compressibility
timeseries-compressibility / src / worker / compressWorker.ts
68 lines · 1.8 KBCodeBlameHistory
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 */
5bab85aRatio-first chart, quantization-floor theory formula, line-segment view, fixed latent dataJeremy Magland 6import { LatentSource } from '../model/latent'
8 initCodecs,
9 compressAll,
11 type BoundResult,
13 ZSTD,
14 ANS,
15 DELTA_ZLIB,
16 DELTA_ZSTD,
17 DELTA_ANS,
20} from '../compress/codecs'
22export interface CompressRequest {
23 id: number
24 kernel: Float64Array
25 sigma: number
26 dither: boolean
27 blockSize: number
e411dffMake LPC order and compression block size controlsJeremy Magland 28 lpcOrder: number
32export interface CompressResponse {
33 id: number
34 results: CodecResult[]
355ddbbAdd per-group entropy limit bars to the compression chartJeremy Magland 35 /** Order-0 entropy limit for each prefilter group. */
36 bounds: BoundResult[]
36e8ceaInteractive explorer for compressibility of quantized filtered Gaussian time seriesJeremy Magland 37 /** Empirical std of the quantized block, for display sanity. */
38 empiricalStd: number
39 error?: string
e411dffMake LPC order and compression block size controlsJeremy Magland 42const PLAIN_CODECS = [ZLIB, ZSTD, ANS, DELTA_ZLIB, DELTA_ZSTD, DELTA_ANS]
44const post = self.postMessage as (message: CompressResponse) => void
46self.onmessage = async (e: MessageEvent<CompressRequest>) => {
e411dffMake LPC order and compression block size controlsJeremy Magland 47 const { id, kernel, sigma, dither, blockSize, lpcOrder, seed } = e.data
49 await initCodecs()
5bab85aRatio-first chart, quantization-floor theory formula, line-segment view, fixed latent dataJeremy Magland 50 const samples = new LatentSource(seed).window(0, blockSize, kernel, sigma, dither)
52 let sumSq = 0
53 for (let i = 0; i < samples.length; i++) {
54 sum += samples[i]
55 sumSq += samples[i] * samples[i]
56 }
57 const mean = sum / samples.length
58 const empiricalStd = Math.sqrt(Math.max(0, sumSq / samples.length - mean * mean))
60 id,
1d751ddOffer LPC orders to 2048, caching the transform across codecsJeremy Magland 61 results: compressAll(samples, [...PLAIN_CODECS, ...lpcCodecs(lpcOrder)]),
e411dffMake LPC order and compression block size controlsJeremy Magland 62 bounds: entropyBounds(samples, lpcOrder),
64 })
355ddbbAdd per-group entropy limit bars to the compression chartJeremy Magland 66 post({ id, results: [], bounds: [], empiricalStd: 0, error: String(err) })
moveopenescclose