summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/dsptricks/dspfunctions.sty
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2023-11-09 03:02:39 +0000
committerNorbert Preining <norbert@preining.info>2023-11-09 03:02:39 +0000
commit3425ad695d75cc781b0fe3412db3aa2ad5985959 (patch)
tree9ec65bca1de4085db47511e67369ea2b71a488fa /graphics/pstricks/contrib/dsptricks/dspfunctions.sty
parentdada699771828d1aa3c78ecf55a2131e5f5799f3 (diff)
CTAN sync 202311090302
Diffstat (limited to 'graphics/pstricks/contrib/dsptricks/dspfunctions.sty')
-rw-r--r--graphics/pstricks/contrib/dsptricks/dspfunctions.sty97
1 files changed, 81 insertions, 16 deletions
diff --git a/graphics/pstricks/contrib/dsptricks/dspfunctions.sty b/graphics/pstricks/contrib/dsptricks/dspfunctions.sty
index ae63eccaa0..c42588a55d 100644
--- a/graphics/pstricks/contrib/dsptricks/dspfunctions.sty
+++ b/graphics/pstricks/contrib/dsptricks/dspfunctions.sty
@@ -11,7 +11,7 @@
%% set of postscript macros to compute the value of various DSP
%% common functions
%%
-%% v1.0, July 2014
+%% v1.1, November 2023
%%
\ProvidesPackage{dspfunctions}[2014/07/24 package for signal processing graphics]
@@ -108,6 +108,27 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
+% \dspAudio{a}{b} zero-DC baseband shape (e.g. an audio signal)
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\def\dspAudio#1#2{%
+ #1 sub #2 div
+ abs
+ dup 1 gt
+ {pop 0}%
+ {dup 0.01 le
+ {pop 0}
+ {dup 0.2 lt
+ {-30 mul}
+ {1 exch sub -8 mul}
+ ifelse
+ 2.7 exch exp 1 add 1 exch div 0.5 sub 2 mul
+ }
+ ifelse}
+ ifelse}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
% Fourier transform of a symmetric 2N+1 tap rect
% \dspSincS{a}{N} sin((x-a)(2N+1)/2)/sin((x-a)/2)
%
@@ -200,12 +221,12 @@
%
% Magnitude response of a generic digital filter defined by the
% constant-coefficient difference equation:
-% y[n] = a_0 x[n] + a_1 x[n-1] + ... + a_{N-1} x[n-N+1]
-% - b_1 y[n-1] - ... - b_{M-1} y[n-M+1]
+% y[n] = b_0 x[n] + b_1 x[n-1] + ... + b_{N-1} x[n-N+1]
+% - a_1 y[n-1] - ... - a_{M-1} y[n-M+1]
%
% The response is computed at the given normalized frequency value
%
-% \dspTFM{a_0 a_1 ... a_{N-1}}{b_1 b_2 ... b_{M-1}}
+% \dspTFM{b_0 b_1 b_2 ... b_{M-1}}{a_0 a_1 ... a_{N-1}}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\dspTFM#1#2{%
@@ -214,17 +235,17 @@
0 % index n
0 % accumulator Re
0 % accumulator Im
- [#1] % coefficients a_n
+ [#1] % coefficients b_n
{ % STACK (neglecting saved input at bottom):
- % x n re im a_n
- dup % x n re im a_n a_n
- 5 index % x n re im a_n a_n x
- 5 index % x n re im a_n a_n x n
- mul dup % x n re im a_n a_n nx nx
- sin exch cos % x n re im a_n a_n sin(nx) cos(nx)
- 4 1 roll mul % x n re im cos(nx) a_n (a_n)sin(nx)
- 3 1 roll mul % x n re im (a_n)sin(nx) (a_n)cos(nx)
- 4 1 roll add % x n (a_n)cos(nx) re im'
+ % x n re im b_n
+ dup % x n re im b_n b_n
+ 5 index % x n re im b_n b_n x
+ 5 index % x n re im b_n b_n x n
+ mul dup % x n re im b_n b_n nx nx
+ sin exch cos % x n re im b_n b_n sin(nx) cos(nx)
+ 4 1 roll mul % x n re im cos(nx) b_n (b_n)sin(nx)
+ 3 1 roll mul % x n re im (b_n)sin(nx) (b_n)cos(nx)
+ 4 1 roll add % x n (b_n)cos(nx) re im'
3 1 roll add exch % x n re' im'
3 2 roll 1 add % x re' im' n'
3 1 roll % x n re im
@@ -233,10 +254,10 @@
dup mul exch dup mul add % (re^2 + im^2)
sqrt % mag of the numerator of transfer function
exch % bring up saved input copy
- 0 % same loop for the b_n coefficients
+ 0 % same loop for the a_n coefficients
0
0
- [#2]
+ [1 #2]
{
dup
5 index
@@ -256,3 +277,47 @@
div %0 eq {pop pop 0} {div} ifelse
}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% Filter the data with a system implementing the transfer function
+% y[n] = b_0 x[n] + b_1 x[n-1] + ... + b_{N-1} x[n-N+1]
+% - a_1 y[n-1] - ... - a_{M-1} y[n-M+1]
+%
+% Use the setFilter macro in the setup part of the drawing command
+% and then \dspFilter{b_0 b_1 b_2 ... b_{M-1}}{a_1 ... a_{N-1}}
+%
+% \dspSignalOpt{\dspSetFilter{b_0 b_1 b_2 ... b_{M-1}}{a_1 ... a_{N-1}}}{x ... \dspFilter}
+%
+% NB: skip a_0 (assumed = 1) from the feedback part!
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\def\dspSetFilter#1#2{%
+ /b [#1] def
+ /xlen b length def
+ /xbuf [xlen {0} repeat] def
+ /a [#2] def
+ /ylen a length def
+ /ybuf [ylen {0} repeat] def
+}
+\def\dspFilter{
+ xbuf aload pop pop xbuf astore /xbuf exch def
+ 0
+ 0 1 xlen 1 sub {
+ dup
+ xbuf exch get
+ exch
+ b exch get
+ mul add
+ } for
+ 0 1 ylen 1 sub {
+ dup
+ ybuf exch get
+ exch
+ a exch get
+ mul sub
+ } for
+ dup
+ ybuf aload pop pop ybuf astore /ybuf exch def
+}
+