September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,66 @@
use Math::Cartesian::Product;
sub deconvolve {
our @g; local *g = shift;
our @f; local *f = shift;
my(@m,@d);
my $h = 1 + @g - @f;
push @m, [(0) x $h, $g[$_]] for 0..$#g;
for my $j (0..$h-1) {
for my $k (0..$#f) {
$m[$j + $k][$j] = $f[$k]
}
}
rref(\@m);
push @d, @{ $m[$_] }[$h] for 0..$h-1;
@d;
}
sub convolve {
our @f; local *f = shift;
our @h; local *h = shift;
my @i;
for my $x (cartesian {@_} [0..$#f], [0..$#h]) {
push @i, @$x[0]+@$x[1];
}
my $cnt = 0;
my @g = (0) x (@f + @h - 1);
for my $x (cartesian {@_} [@f], [@h]) {
$g[$i[$cnt++]] += @$x[0]*@$x[1];
}
@g;
}
sub rref {
our @m; local *m = shift;
@m or return;
my ($lead, $rows, $cols) = (0, scalar(@m), scalar(@{$m[0]}));
foreach my $r (0 .. $rows - 1) {
$lead < $cols or return;
my $i = $r;
until ($m[$i][$lead])
{++$i == $rows or next;
$i = $r;
++$lead == $cols and return;}
@m[$i, $r] = @m[$r, $i];
my $lv = $m[$r][$lead];
$_ /= $lv foreach @{ $m[$r] };
my @mr = @{ $m[$r] };
foreach my $i (0 .. $rows - 1)
{$i == $r and next;
($lv, my $n) = ($m[$i][$lead], -1);
$_ -= $lv * $mr[++$n] foreach @{ $m[$i] };}
++$lead;}
}
my @h = qw<-8 -9 -3 -1 -6 7>;
my @f = qw<-3 -6 -1 8 -6 3 -1 -9 -9 3 -2 5 2 -2 -7 -1>;
print ' conv(f,h) = g = ' . join(' ', my @g = convolve(\@f, \@h)) . "\n";
print 'deconv(g,f) = h = ' . join(' ', deconvolve(\@g, \@f)) . "\n";
print 'deconv(g,h) = f = ' . join(' ', deconvolve(\@g, \@h)) . "\n";

View file

@ -0,0 +1,38 @@
import numpy
h = [-8,-9,-3,-1,-6,7]
f = [-3,-6,-1,8,-6,3,-1,-9,-9,3,-2,5,2,-2,-7,-1]
g = [24,75,71,-34,3,22,-45,23,245,25,52,25,-67,-96,96,31,55,36,29,-43,-7]
# https://stackoverflow.com/questions/14267555/find-the-smallest-power-of-2-greater-than-n-in-python
def shift_bit_length(x):
return 1<<(x-1).bit_length()
def conv(a, b):
p = len(a)
q = len(b)
n = p + q - 1
r = shift_bit_length(n)
y = numpy.fft.ifft(numpy.fft.fft(a,r) * numpy.fft.fft(b,r),r)
return numpy.trim_zeros(numpy.around(numpy.real(y),decimals=6))
def deconv(a, b):
p = len(a)
q = len(b)
n = p - q + 1
r = shift_bit_length(max(p, q))
y = numpy.fft.ifft(numpy.fft.fft(a,r) / numpy.fft.fft(b,r), r)
return numpy.trim_zeros(numpy.around(numpy.real(y),decimals=6))
# should return g
print(conv(h,f))
# should return h
print(deconv(g,f))
# should return f
print(deconv(g,h))