[qwaq] Add a little z-transform program

And fix an error in floatview.

The z-transform program is so I can test out my math and eventually
develop some hopefully interesting sound generators.
This commit is contained in:
Bill Currie 2021-06-19 11:25:05 +09:00
parent 8db1957452
commit 12ab283c22
3 changed files with 60 additions and 2 deletions

View file

@ -2,7 +2,7 @@ QWAQ_LIBS=@QWAQ_LIBS@
QWAQ_DEPS=@QWAQ_DEPS@
QWAQ_INCS=@QWAQ_INCS@
noinst_PROGRAMS += @QWAQ_TARGETS@ ruamoko/qwaq/qwaq-app.dat$(EXEEXT) ruamoko/qwaq/gcd.dat$(EXEEXT)
noinst_PROGRAMS += @QWAQ_TARGETS@ ruamoko/qwaq/qwaq-app.dat$(EXEEXT) ruamoko/qwaq/gcd.dat$(EXEEXT) ruamoko/qwaq/z-transform.dat$(EXEEXT)
qwaq_app_dat_src= \
ruamoko/qwaq/qwaq-app.r \
@ -103,6 +103,14 @@ ruamoko/qwaq/gcd.dat$(EXEEXT): $(ruamoko_qwaq_gcd_obj) $(QFCC_DEP) ruamoko/lib/l
include $(ruamoko_qwaq_gcd_dep) # am--include-marker
r_depfiles_remade += $(ruamoko_qwaq_gcd_dep)
ruamoko_qwaq_z_transform_dat_SOURCES=ruamoko/qwaq/z-transform.r
ruamoko_qwaq_z_transform_obj=$(ruamoko_qwaq_z_transform_dat_SOURCES:.r=.o)
ruamoko_qwaq_z_transform_dep=$(call qcautodep,$(ruamoko_qwaq_z_transform_dat_SOURCES:.o=.Qo))
ruamoko/qwaq/z-transform.dat$(EXEEXT): $(ruamoko_qwaq_z_transform_obj) $(QFCC_DEP) ruamoko/lib/libcsqc.a ruamoko/lib/libr.a
$(V_QFCCLD)$(QLINK) -o $@ $(ruamoko_qwaq_z_transform_obj) -lcsqc -lr
include $(ruamoko_qwaq_z_transform_dep) # am--include-marker
r_depfiles_remade += $(ruamoko_qwaq_z_transform_dep)
EXTRA_PROGRAMS += ruamoko/qwaq/qwaq-curses ruamoko/qwaq/qwaq-x11
EXTRA_DIST += \
$(qwaq_dat_src) \

View file

@ -20,7 +20,7 @@
-draw
{
[super draw];
string val = sprintf ("%.9", data[0]);
string val = sprintf ("%.9g", data[0]);
[self mvprintf:{0, 0}, "%*.*s", xlen, xlen, val];
return self;
}

View file

@ -0,0 +1,50 @@
#include <math.h>
void printf (string fnt, ...) = #0;
#define SAMPLES 4
float output[SAMPLES];
float input[SAMPLES];
float T=0.1;
float w=2;
float a=0.5;
float A[3], B[3];
void
z_transform (float *y, float *x, float *A, float *B, int n, int zind)
{
zind %= SAMPLES;
float c = x[zind] * A[0];
for (int i = 1; i < n; i++) {
int z = (SAMPLES + zind - i) % SAMPLES;
c += x[z] * A[i] - y[z] * B[i];
}
y[zind] = c / B[0];
}
int
main ()
{
float e = exp (-a*T);
float c = cos (w*T);
float s = sin (w*T);
B[0] = 1;
B[1] = -2 * e * c;
B[2] = e * e;
A[0] = 0;
A[1] = 1 - e*(c + (a/w)*s);
A[2] = e*e + e*((a/w)*s - c);
for (int i = 0; i < 200; i++) {
int ind = i % SAMPLES;
input[ind] = 1;//i ? 0 : 1;
z_transform (output, input, A, B, 3, i);
printf ("%2d %7.4f %7.4f\n", i, input[ind], output[ind]);
}
return 0;
}