hsignal-0.1.0.3: lib/Numeric/Signal/Multichannel.hs
{-# OPTIONS_GHC -fglasgow-exts #-}
-----------------------------------------------------------------------------
-- |
-- Module : Numeric.Signal.Multichannel
-- Copyright : (c) Alexander Vivian Hugh McPhail 2010
-- License : GPL-style
--
-- Maintainer : haskell.vivian.mcphail <at> gmail <dot> com
-- Stability : provisional
-- Portability : uses Concurrency
--
-- Signal processing functions, multichannel datatype
--
-- link with '-threaded' and run with +RTS Nn, where n is the number of CPUs
--
-----------------------------------------------------------------------------
module Numeric.Signal.Multichannel (
Multichannel,
mapConcurrently
) where
-----------------------------------------------------------------------------
--import qualified Numeric.Signal as S
--import Complex
import qualified Data.Array.IArray as I
import Control.Concurrent
import Control.Concurrent.MVar
import System.IO.Unsafe(unsafePerformIO)
--import qualified Data.List as L
import Data.Packed.Vector
--import Data.Packed(Container(..))
import Foreign.Storable
--import Numeric.GSL.Vector
--import Numeric.LinearAlgebra.Algorithms
--import qualified Numeric.GSL.Fourier as F
--import Prelude hiding(filter)
-----------------------------------------------------------------------------
data Multichannel a = MC {
_sampling_rate :: Int -- ^ sampling rate
, _precision :: Int -- ^ bits of precision
, _channels :: Int -- ^ number of channels
, _length :: Int -- ^ length in samples
, _data :: I.Array Int (Vector a) -- ^ data
}
-----------------------------------------------------------------------------
-- | map a function executed concurrently
mapConcurrently :: Storable b => Multichannel a -- ^ input data
-> (Vector a -> Vector b) -- ^ the function to be mapped
-> Multichannel b -- ^ output data
mapConcurrently (MC sr p c _ d) f = unsafePerformIO $ do
results <- newMVar []
mapM_ (forkIO . applyFunction results f) $ I.assocs d
vectors <- takeMVar results
return $ MC sr p c (dim $ snd $ head vectors) (I.array (1,c) vectors)
where applyFunction results f' (i,e) = do
let o = f' e
modifyMVar_ results (\x -> return ((i,o):x))
-----------------------------------------------------------------------------