-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNoise.hs
77 lines (69 loc) · 2.51 KB
/
Noise.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ConstraintKinds #-}
module Noise(
histogram
, Structure(..)
, quantizationNoise
) where
import qualified Statistics.Sample.Histogram as H
import qualified Data.Vector.Unboxed as U
import Plot
import Graphics.PDF
import Common
import Generators
import Signal
import System.Random
import Text.Printf
nbBins = 20
drawHist :: Int
-> U.Vector Double
-> U.Vector Double
-> Int
-> Int
-> CoordinateMapping Double Double
-> Draw ()
drawHist nb v b wi hi (toPoint,_) = do
let binWidth :: Double
binWidth = fromIntegral wi / fromIntegral nb
d = binWidth / 2.0
drawBin p = do
fillColor (Rgb 0.8 0.8 1.0)
let (x :+ y) = toPoint p
r = Rectangle ((x - d) :+ 0) ((x + d) :+ y)
fill r
stroke r
return ()
mapM_ drawBin $ (U.toList $ U.zip v b)
histogram :: (U.Unbox a, HasDoubleRepresentation a)
=> [a]
-> StyledSignal Double Double
histogram l = do
let (s,v1) = H.histogram nbBins (U.fromList . map toDouble $ l)
v = U.map (/ (U.sum v1)) v1
nb = U.length s
mas = U.maximum s
mis = U.minimum s
d = (mas - mis) / fromIntegral nb / 2.0
detailed x = printf "%.2g" x
style = defaultPlotStyle { title = Just "Histogram"
, horizontalLabel = Nothing
, verticalLabel = Nothing
, horizontalBounds = Just (mis-d,mas+d)
, verticalBounds = Just (U.minimum v, U.maximum v)
, epilog = drawHist nb s v
, axis = False
, horizontalTickRepresentation = detailed
}
discreteSignalsWithStyle (U.length s) style []
class Structure m where
doubleVersion :: (Sample i, Sample o) => m f i o -> m Double Double Double
transferFunction :: (Sample i, Sample o) => m f i o -> Signal i -> Signal o
quantizationNoise :: (Structure m , Random i, Sample o, Sample i)
=> Signal i
-> m p i o
-> IO (Signal Double)
quantizationNoise r structure = do
let ds = transferFunction (doubleVersion structure) (mapS toDouble r)
fs = mapS toDouble . transferFunction structure $ r
return $ zipWithS (-) ds fs