From 5ba8548c23908bd78c9581a023aaef0965aeb142 Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Tue, 8 Jan 2013 15:33:42 +0000 Subject: [PATCH] Make halveFrequency return a Maybe. --- OFC/Spectral.hs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/OFC/Spectral.hs b/OFC/Spectral.hs index 0464f8c..62330b6 100644 --- a/OFC/Spectral.hs +++ b/OFC/Spectral.hs @@ -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 -- 2.47.3