]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Make halveFrequency return a Maybe.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Tue, 8 Jan 2013 15:33:42 +0000 (15:33 +0000)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Tue, 8 Jan 2013 15:33:42 +0000 (15:33 +0000)
OFC/Spectral.hs

index 0464f8cca0adb8cce986a730289f0a2ff54c78c8..62330b6d4831e6000db9ba71c68974a6cd2be95d 100644 (file)
@@ -82,11 +82,11 @@ divideFrequency f1 f2 = fromIntegral $ sign * absDiv f1 f2
   sign = if (f1 >= 0 && f2 >= 0) || (f1 < 0 && f2 < 0)
          then 1 else (-1)
 
-halveFrequency :: Frequency -> Frequency
-halveFrequency freq@(Frequency m o) =
+halveFrequency :: Frequency -> Maybe Frequency
+halveFrequency (Frequency m o) =
   if m `mod` 2 == 0
-  then Frequency (m `div` 2) (o `div` 2)
-  else error $ "Cannot halve frequency " ++ show freq
+  then Just $ Frequency (m `div` 2) (o `div` 2)
+  else Nothing
 
 data Band =
   Band Frequency Frequency 
@@ -157,7 +157,9 @@ wrapContent :: Frequency -> (Band, ContentType) -> [(Band, ContentType)]
 wrapContent samplingRate (Band low high, content) = 
   mergeContent $ undamaged:(damagedPositive ++ damagedNegative)
   where
-  nyquist = halveFrequency samplingRate
+  nyquist = case halveFrequency samplingRate of
+    Just f -> f
+    Nothing -> error $ "wrapContent cannot halve sampling rate " ++ show samplingRate
   numFreqs = nyquist + 1
   negNyquist = - nyquist
   undamaged = (bandIntersection (Band low high) (Band negNyquist numFreqs), content)
@@ -179,7 +181,9 @@ wrapBand samplingRate (Band low high) =
     laterBands = if localHigh == newHigh
       then []
       else wrapBand' (Band 0 (newHigh - samplingRate))
-    nyquist = halveFrequency samplingRate
+    nyquist = case halveFrequency samplingRate of
+      Just f -> f
+      Nothing -> error $ "wrapBand cannot halve sampling rate " ++ show samplingRate
     numFreqs = nyquist + 1
     offsetFrequency = samplingRate `multiplyFrequency` (low `divideFrequency` samplingRate)
     newLow = low - offsetFrequency