module Synthesizer.Plain.Analysis (
volumeMaximum,
volumeEuclidean,
volumeEuclideanSqr,
volumeSum,
volumeVectorMaximum,
volumeVectorEuclidean,
volumeVectorEuclideanSqr,
volumeVectorSum,
bounds,
histogramDiscreteArray,
histogramLinearArray,
histogramDiscreteIntMap,
histogramLinearIntMap,
histogramIntMap,
directCurrentOffset,
scalarProduct,
centroid,
centroidAlt,
firstMoment,
average,
rectify,
zeros,
BinaryLevel(Low, High),
binaryLevelFromBool,
binaryLevelToNumber,
flipFlopHysteresis,
flipFlopHysteresisStep,
chirpTransform,
binarySign,
deltaSigmaModulation,
deltaSigmaModulationPositive,
spread,
) where
import qualified Synthesizer.Plain.Signal as Sig
import qualified Synthesizer.Plain.Control as Ctrl
import qualified Synthesizer.Plain.Filter.Recursive.Integration as Integration
import qualified Data.NonEmpty as NonEmpty
import qualified Data.Array as Array
import qualified Data.IntMap as IntMap
import Data.Tuple.HT (sortPair)
import Data.Array (accumArray)
import Data.List (foldl', )
import qualified Algebra.Algebraic as Algebraic
import qualified Algebra.RealField as RealField
import qualified Algebra.Field as Field
import qualified Algebra.RealRing as RealRing
import qualified Algebra.Absolute as Absolute
import qualified Algebra.Ring as Ring
import qualified Algebra.Additive as Additive
import qualified Algebra.NormedSpace.Maximum as NormedMax
import qualified Algebra.NormedSpace.Euclidean as NormedEuc
import qualified Algebra.NormedSpace.Sum as NormedSum
import NumericPrelude.Numeric
import NumericPrelude.Base
volumeMaximum :: (RealRing.C y) => Sig.T y -> y
volumeMaximum =
foldl max zero . rectify
volumeEuclidean :: (Algebraic.C y) => Sig.T y -> y
volumeEuclidean =
Algebraic.sqrt . volumeEuclideanSqr
volumeEuclideanSqr :: (Field.C y) => Sig.T y -> y
volumeEuclideanSqr =
average . map sqr
volumeSum :: (Absolute.C y, Field.C y) => Sig.T y -> y
volumeSum = average . rectify
volumeVectorMaximum :: (NormedMax.C y yv, Ord y) => Sig.T yv -> y
volumeVectorMaximum =
NormedMax.norm
volumeVectorEuclidean :: (Algebraic.C y, NormedEuc.C y yv) => Sig.T yv -> y
volumeVectorEuclidean =
Algebraic.sqrt . volumeVectorEuclideanSqr
volumeVectorEuclideanSqr :: (Field.C y, NormedEuc.Sqr y yv) => Sig.T yv -> y
volumeVectorEuclideanSqr =
average . map NormedEuc.normSqr
volumeVectorSum :: (NormedSum.C y yv, Field.C y) => Sig.T yv -> y
volumeVectorSum =
average . map NormedSum.norm
bounds :: Ord y => NonEmpty.T Sig.T y -> (y,y)
bounds (NonEmpty.Cons x xs) =
foldl' (\(minX,maxX) y -> (min y minX, max y maxX)) (x,x) xs
histogramDiscreteArray :: NonEmpty.T Sig.T Int -> (Int, Sig.T Int)
histogramDiscreteArray x =
let hist =
accumArray (+) zero
(bounds x) (attachOne $ NonEmpty.flatten x)
in (fst (Array.bounds hist), Array.elems hist)
histogramLinearArray :: RealField.C y => NonEmpty.T Sig.T y -> (Int, Sig.T y)
histogramLinearArray (NonEmpty.Cons x []) = (floor x, [])
histogramLinearArray x =
let (xMin,xMax) = bounds x
hist =
accumArray (+) zero
(floor xMin, floor xMax)
(meanValues x)
in (fst (Array.bounds hist), Array.elems hist)
histogramDiscreteIntMap :: NonEmpty.T Sig.T Int -> (Int, Sig.T Int)
histogramDiscreteIntMap x =
let hist = IntMap.fromListWith (+) (attachOne $ NonEmpty.flatten x)
in case IntMap.toAscList hist of
[] -> error "histogramDiscreteIntMap: the list was non-empty before processing ..."
fAll@((fIndex,fHead):fs) -> (fIndex, fHead :
concat (zipWith
(\(i0,_) (i1,f1) -> replicate (i1i01) zero ++ [f1])
fAll fs))
histogramLinearIntMap :: RealField.C y => NonEmpty.T Sig.T y -> (Int, Sig.T y)
histogramLinearIntMap (NonEmpty.Cons x []) = (floor x, [])
histogramLinearIntMap x =
let hist = IntMap.fromListWith (+) (meanValues x)
(startKey:_, elems) = unzip (IntMap.toAscList hist)
in (startKey, elems)
histogramIntMap :: (RealField.C y) => y -> NonEmpty.T Sig.T y -> (Int, Sig.T Int)
histogramIntMap binsPerUnit =
histogramDiscreteIntMap . quantize binsPerUnit
quantize :: (Functor f, RealField.C y) => y -> f y -> f Int
quantize binsPerUnit = fmap (floor . (binsPerUnit*))
attachOne :: Sig.T i -> Sig.T (i,Int)
attachOne = map (\i -> (i,one))
meanValues :: RealField.C y => NonEmpty.T Sig.T y -> [(Int,y)]
meanValues = concatMap spread . NonEmpty.mapAdjacent (,)
spread :: RealField.C y => (y,y) -> [(Int,y)]
spread lr0 =
let (l,r) = sortPair lr0
(li,lf) = splitFraction l
(ri,rf) = splitFraction r
k = recip (rl)
nodes =
(li,k*(1lf)) :
zip [li+1 ..] (replicate (rili1) k) ++
(ri, k*rf) :
[]
in if li==ri
then [(li,one)]
else nodes
directCurrentOffset :: Field.C y => Sig.T y -> y
directCurrentOffset = average
scalarProduct :: Ring.C y => Sig.T y -> Sig.T y -> y
scalarProduct xs ys =
sum (zipWith (*) xs ys)
centroid :: Field.C y => Sig.T y -> y
centroid xs =
firstMoment xs / sum xs
centroidAlt :: Field.C y => Sig.T y -> y
centroidAlt xs =
sum (scanr (+) zero (tail xs)) / sum xs
firstMoment :: Ring.C y => Sig.T y -> y
firstMoment =
scalarProduct (iterate (one+) zero)
average :: Field.C y => Sig.T y -> y
average x =
sum x / fromIntegral (length x)
rectify :: Absolute.C y => Sig.T y -> Sig.T y
rectify = map abs
zeros :: (Ord y, Ring.C y) => Sig.T y -> Sig.T Bool
zeros xs =
let signs = map (>=zero) xs
in zipWith (/=) signs (tail signs)
data BinaryLevel = Low | High
deriving (Eq, Show, Enum)
binaryLevelFromBool :: Bool -> BinaryLevel
binaryLevelFromBool False = Low
binaryLevelFromBool True = High
binaryLevelToNumber :: Ring.C a => BinaryLevel -> a
binaryLevelToNumber Low = negate one
binaryLevelToNumber High = one
flipFlopHysteresis :: (Ord y) =>
(y,y) -> BinaryLevel -> Sig.T y -> Sig.T BinaryLevel
flipFlopHysteresis bnds = scanl (flipFlopHysteresisStep bnds)
flipFlopHysteresisStep :: Ord a => (a, a) -> BinaryLevel -> a -> BinaryLevel
flipFlopHysteresisStep (lower,upper) =
\state x ->
binaryLevelFromBool $
case state of
High -> not(x<lower)
Low -> x>upper
chirpTransform :: Ring.C y =>
y -> Sig.T y -> Sig.T y
chirpTransform z xs =
map (scalarProduct xs) $
map (\zn -> Ctrl.curveMultiscaleNeutral (*) zn one) $
Ctrl.curveMultiscaleNeutral (*) z one
binarySign ::
(Ord y, Additive.C y) => Sig.T y -> Sig.T BinaryLevel
binarySign =
map (binaryLevelFromBool . (zero <=))
deltaSigmaModulation ::
RealRing.C y => Sig.T y -> Sig.T BinaryLevel
deltaSigmaModulation x =
let y = binarySign (Integration.run (x (zero : map binaryLevelToNumber y)))
in y
deltaSigmaModulationPositive ::
RealRing.C y => y -> Sig.T y -> Sig.T y
deltaSigmaModulationPositive threshold x =
let y =
map (\xi -> if xi>=threshold then threshold else zero) $
Integration.run (x (zero:y))
in y