Mercurial > rlm
diff src/rlm/thermal_noise.clj @ 0:78a630e650d2
initial import
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 00:57:08 -0700 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/rlm/thermal_noise.clj Tue Oct 18 00:57:08 2011 -0700 1.3 @@ -0,0 +1,84 @@ 1.4 +(ns rlm.thermal-noise 1.5 + (:refer-clojure :only []) 1.6 + (:require rlm.ns-rlm mobius.base)) 1.7 +(rlm.ns-rlm/ns-clone mobius.base) 1.8 + 1.9 +(defrecord imaginary [real complex]) 1.10 + 1.11 +(defmethod + [imaginary Number] [i r] 1.12 + (imaginary. (+ (:real i) r) (:complex i))) 1.13 +(defmethod + [Number imaginary] [r i] 1.14 + (+ i r)) 1.15 +(defmethod + [imaginary imaginary] [i1 i2] 1.16 + (imaginary. (+ (:real i1) (:real i2)) 1.17 + (+ (:complex i1) (:complex i2)))) 1.18 + 1.19 + 1.20 +(defmethod * [imaginary Number] [i r] 1.21 + (imaginary. (* r (:real i)) (* r (:complex i)))) 1.22 +(defmethod * [Number imaginary] [r i] 1.23 + (* i r)) 1.24 +(defmethod * [imaginary imaginary] [i1 i2] 1.25 + (let [[a1 b1] [(:real i1) (:complex i1)] 1.26 + [a2 b2] [(:real i2) (:complex i2)]] 1.27 + (imaginary. (- (* a1 a2) (* b1 b2)) 1.28 + (+ (* a1 b2) (* a2 b1))))) 1.29 + 1.30 +(defmethod / imaginary [i] 1.31 + 1.32 + (cond (and (not= (:real i) 0) (not= (:complex i) 0)) 1.33 + (let [[a b] [(:real i) (:complex i)] 1.34 + d (- (/ (/ a) (+ (/ a b) (/ b a)))) 1.35 + c (/ (+ 1 (* b d)) a)] 1.36 + (imaginary. c d)) 1.37 + (= (:real i) 0) 1.38 + (imaginary. 0 (- (/ (:complex i)))) 1.39 + (= (:complex i) 0) 1.40 + (imaginary. (/ (:real i)) 0))) 1.41 + 1.42 + 1.43 +(defmethod / [imaginary imaginary] [i1 i2] 1.44 + (* i1 (/ i2))) 1.45 + 1.46 +(defmethod / [imaginary Number] [i r] 1.47 + (* i (/ r))) 1.48 + 1.49 +(defmethod / [Number imaginary] [r i] 1.50 + (* r (/ i))) 1.51 + 1.52 +(defmulti norm class) 1.53 +(defmethod norm Number [r] r) 1.54 +(defmethod norm imaginary [i] 1.55 + (sqrt (+ (sq (:real i) ) (sq (:complex i))))) 1.56 + 1.57 + 1.58 + 1.59 + 1.60 +(def i (imaginary. 0 1)) 1.61 + 1.62 +(defn h** [w_0 v_0 y w] 1.63 + (/ v_0 (+ (- (sq w)) (* 2 y i w) (sq w_0)))) 1.64 + 1.65 +(def f_0 10e3) 1.66 +(def w_0 (* f_0 2 Math/PI)) 1.67 +(def v_0 0.01) 1.68 +(def h* (partial h** w_0 v_0)) 1.69 + 1.70 + 1.71 +(def x (range 1 1e7 100)) 1.72 + 1.73 +(def y-range [0.1 0.5 1 2 5 10 100 1000]) 1.74 + 1.75 + 1.76 +(defn graphs [] 1.77 + (let [plot (xy-plot)] 1.78 + (dorun 1.79 + (for [ y y-range] 1.80 + (let [result (vec (map (partial (comp norm h*) y) x))] 1.81 + (add-lines plot (vec (map log10 x)) (vec (map log10 result)))))) 1.82 + (set-x-label plot "log frequency") 1.83 + (set-y-label plot "log h*"))) 1.84 + 1.85 +(def rrr (imaginary. 1 1)) 1.86 + 1.87 +