shithub: leaf

Download patch

ref: 007893b34b10c827ea7698adecaf6ead27e2f974
parent: 2dcb4a2f94869c2a19325b8371ae2b010aca06e6
author: Matthew Wang <Matthew@nat-oitwireless-inside-vapornet100-10-9-78-40.princeton.edu>
date: Tue Sep 24 14:07:22 EDT 2019

partially working wavefolder

--- a/LEAF/Inc/leaf-wavefolder.h
+++ b/LEAF/Inc/leaf-wavefolder.h
@@ -37,7 +37,7 @@
 {
     double Ln1;
     double Fn1;
-    double xn1;
+    float xn1;
     
 } tLockhartWavefolder;
 
--- a/LEAF/Src/leaf-wavefolder.c
+++ b/LEAF/Src/leaf-wavefolder.c
@@ -20,94 +20,90 @@
 {
     w->Ln1 = 0.0;
     w->Fn1 = 0.0;
-    w->xn1 = 0.0;
+    w->xn1 = 0.0f;
 }
 
 double tLockhartWavefolderLambert(double x, double ln)
 {
-    double w = ln;
-    double expw, p, r, s, err, q;
+    double thresh, w, expw, p, r, s, err;
+    // Error threshold
+    thresh = 10e-6;
+    // Initial guess (use previous value)
+    w = ln;
     
-    for (int i = 0; i < 1000; i++)
-    {
-        //err = x * (logf(x) - logf(w));
+    // Haley's method (Sec. 4.2 of the paper)
+    for(int i=0; i<100; i+=1) {
         
-        /*
-         r = logf(x / w) - w;
-         q = 2.0f * (1.0f+w) * (1.0f + w + 0.666666666f*r);
-         err = (r / (1.0f+w)) * ((q - r) / (q - 2*r));
-         */
         expw = exp(w);
         
-        p = (w * expw) - x;
-        r = (w + 1.0) * expw;
-        s = (w + 2.0) / (2.0 * (w + 1.0));
-        err = (p/(r-(p*s)));
+        p = w*expw - x;
+        r = (w+1.0)*expw;
+        s = (w+2.0)/(2.0*(w+1.0));        err = (p/(r-(p*s)));
         
-        if (fabs(err) < THRESH) break;
+        if (fabs(err)<thresh) {
+            break;
+        }
+        if (isnan(err))
+        {
+            break;
+        }
         
-        w -= err;
-        //w = w * (1 + err);
-        //w = w - err;
+        w = w - err;
     }
-    
     return w;
 }
 
 float tLockhartWavefolder_tick(tLockhartWavefolder* const w, float samp)
 {
-    int l;
-    double u, Ln, Fn, xn;
-    float o;
     
-    samp=(double)samp;
-    // Compute Antiderivative
-    if (samp > 0) l = 1;
-    else if (samp < 0) l = -1;
-    else l = 0;
+    float out = 0.0f;
+    // Constants
+    double RL = 7.5e3;
+    double R = 15e3;
+    double VT = 26e-3;
+    double Is = 10e-16;
     
-    u = LOCKHART_D * exp(l*LOCKHART_B*samp);
-    Ln = tLockhartWavefolderLambert(u, w->Ln1);
-    Fn = (0.5 * VT_DIV_B) * (Ln*(Ln + 2.0)) - 0.5*LOCKHART_A*samp*samp;
+    double a = 2.0*RL/R;
+    double b = (R+2.0*RL)/(VT*R);
+    double d = (RL*Is)/VT;
     
+    // Antialiasing error threshold
+    double thresh = 10e-10;
+    
+    // Compute Antiderivative
+    int l = (samp > 0) - (samp < 0);
+    double u = d*exp(l*b*samp);
+    double Ln = tLockhartWavefolderLambert(u,w->Ln1);
+    double Fn = (0.5*VT/b)*(Ln*(Ln + 2.0)) - 0.5*a*samp*samp;
+    
     // Check for ill-conditioning
-    if (fabs(samp - w->xn1) < ILL_THRESH)
-    {
+    if (fabs(samp-w->xn1)<thresh) {
+        
         // Compute Averaged Wavefolder Output
-        xn = 0.5*(samp + w->xn1);
-        u = LOCKHART_D*exp(l*LOCKHART_B*xn);
-        Ln = tLockhartWavefolderLambert(u, w->Ln1);
-        o = (float)((l*LOCKHART_VT*Ln) - (LOCKHART_A*xn));
+        double xn = 0.5*(samp+w->xn1);
+        u = d*exp(l*b*xn);
+        Ln = tLockhartWavefolderLambert(u,w->Ln1);
+        out = (float) (l*VT*Ln - a*xn);
+        if (isnan(out))
+        {
+            ;
+        }
+        
     }
-    else
-    {
+    else {
+        
         // Apply AA Form
-        o = (float)((Fn - w->Fn1)/(samp - w->xn1));
+        out = (float) ((Fn-w->Fn1)/(samp-w->xn1));
+        if (isnan(out))
+        {
+            ;
+        }
     }
     
-    /*
-     // Check for ill-conditioning
-     if (abs(in1-xn1)<thresh) {
-     
-     // Compute Averaged Wavefolder Output
-     xn = 0.5*(in1+xn1);
-     u = d*pow(e,l*b*xn);
-     Ln = Lambert_W(u,Ln1);
-     out1 = l*VT*Ln - a*xn;
-     
-     }
-     else {
-     
-     // Apply AA Form
-     out1 = (Fn-Fn1)/(in1-xn1);
-     
-     }
-     */
-    
     // Update States
     w->Ln1 = Ln;
     w->Fn1 = Fn;
     w->xn1 = samp;
     
-    return o;
+    return out;
 }
--- a/LEAF_JUCEPlugin/Source/MyTest.cpp
+++ b/LEAF_JUCEPlugin/Source/MyTest.cpp
@@ -20,10 +20,11 @@
 tNoise noise;
 tCycle sine;
 tFIR filter;
+tLockhartWavefolder wf;
 
 float gain;
 bool buttonState;
-int ratio = 16;
+int ratio = 2;
 
 void    LEAFTest_init            (float sampleRate, int blockSize)
 {
@@ -33,31 +34,30 @@
     tNoise_init(&noise, WhiteNoise);
     tCycle_init(&sine);
     tCycle_setFreq(&sine, 220);
+    tLockhartWavefolder_init(&wf);
     leaf_pool_report();
 }
 
-float nothing(float val) {
-    return val;
-}
-
 float   LEAFTest_tick            (float input)
 {
     float sample = tCycle_tick(&sine);
     float output[ratio];
     
-    if (buttonState) {
-        tOversampler_upsample(&os, sample, output);
-        for (int i = 0; i < ratio; ++i) {
-            output[i] *= gain*10.0f;
-            output[i] = LEAF_clip(-1.0f, output[i], 1.0f);
-        }
-        sample = tOversampler_downsample(&os, output);
-    }
-    else {
-        sample *= gain*10.0f;
-        sample = LEAF_clip(-1.0f, sample, 1.0f);
-    }
-    return sample * gain;
+    sample *= gain*10.0f;
+    sample = tLockhartWavefolder_tick(&wf, sample);
+//    if (buttonState) {
+//        tOversampler_upsample(&os, sample, output);
+//        for (int i = 0; i < ratio; ++i) {
+//            output[i] *= gain*10.0f;
+//            output[i] = tLockhartWavefolder_tick(&wf, output[i]);
+//        }
+//        sample = tOversampler_downsample(&os, output);
+//    }
+//    else {
+//        sample *= gain*10.0f;
+//        sample = tLockhartWavefolder_tick(&wf, sample);
+//    }
+    return sample;
 }