本日のボツスクリプト

YAPC::ASIA Tokyo 2011で「Perl meets beats.」を話したときに、
「フィルターはあるのかな?」みたいなツイートを見かけて以来、
ずっとやらなきゃなーって思ってて、結果的に作り直してるんだけど、
そのうち、フィルターの部分はできたと思う。

リポジトリはこちら。
https://github.com/techno-cat/p5-Cassis

んでもって、ボツスクリプト。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use v5.14;
use strict;
use warnings;
 
use Cassis;
use Math::Trig qw(pi tan);
use List::Util qw(max min);
use constant N => 1000;
 
use Imager;
 
# see also "Imager::Font"
my $font_size = 12;
my $font = Imager::Font->new(
    file => '/Library/Fonts/Osaka.ttf' ) or die;
 
my $cutoff = 0.2;
my $q = 2.5;
 
my @graph_params = (
    { type => 'LPF', params => Cassis::Iir2::LPF->new( cutoff => $cutoff, q => $q )->params() },
    { type => 'HPF', params => Cassis::Iir2::HPF->new( cutoff => $cutoff, q => $q )->params() },
    { type => 'BPF', params => Cassis::Iir2::BPF->new( cutoff => $cutoff, q => $q )->params() },
    { type => 'BEF', params => Cassis::Iir2::BEF->new( cutoff => $cutoff, q => $q )->params() }
);
 
foreach ( @graph_params ) {
    my $type = $_->{type};
    my $params = $_->{params};
    my $title = sprintf( "%s ( Cutoff: %.3f, Q: %.3f )", $type, $cutoff, $q );
 
    print "=== $title ===\n";
    foreach my $key ( qw/b0 b1 b2 a1 a2/ ) {
        printf( "%s: %6.3f\n", $key, $params->{$key} );
    }
 
    my @graph_src = map {
        my $f = $_ / N;
        my $gain = calc_gain( $f, $params );
        { f => $f, gain => $gain };
    } 0..(N / 2);
 
    my $img = draw_amplitude_spectrum( \@graph_src, $title );
    $img->write( file => "amplitude_spectrum_$type.png" ) or die $img->errstr;
}
 
sub draw_amplitude_spectrum {
    my ( $graph_src, $title ) = @_;
 
    my %margin = ( left => 30, top => 20, right => 20, bottom => 20 );
    my $tick_gain = 100;
    my $graph_width = scalar(@{$graph_src}) + 1;
    my $graph_height = 400 + 1;
    my $width = $margin{left} + $graph_width + 1 + $margin{right};
    my $height = $margin{top} + $graph_height + 1 + $margin{bottom};
 
    my $img = Imager->new(
        xsize => $width, ysize => $height );
    $img->box( filled => 1, color => 'black' );
 
    $img->align_string(
        x => $width / 2,
        y => 2,
        halign => 'center',
        valign => 'top',
        string => $title,
        font => $font,
        color => 'white',
        size => $font_size );
 
    my $x0 = $margin{left};
    my $y0 = $margin{top} + $graph_height - 1;
 
    my $x = 0;
    my $x_step = ($graph_width - 1) / 10;
    while ( $x < $graph_width ) {
        $img->line( color => 'white',
            x1 => $x0 + $x, y1 => $y0,
            x2 => $x0 + $x, y2 => $y0 - ($graph_height - 1) );
 
        $img->align_string(
            x => $x0 + $x,
            y => $y0 + 5,
            halign => 'center',
            valign => 'top',
            string => sprintf( "%.2f", ($x / ($graph_width - 1)) * 0.5 ),
            font => $font,
            color => 'white',
            size => $font_size );
 
        $x += $x_step;
    }
 
    my $y = 0;
    while ( $y < $graph_height ) {
        $img->line( color => 'white',
            x1 => $x0,                      y1 => $y0 - $y,
            x2 => $x0 + ($graph_width - 1), y2 => $y0 - $y );
 
        if ( 0 < $y ) {
            $img->align_string(
                x => $x0 - 5,
                y => $y0 - $y + int($font_size * 0.4),
                halign => 'right',
                valign => 'baseline',
                string => sprintf( "%.1f", $y / $tick_gain ),
                font => $font,
                color => 'white',
                size => $font_size );
        }
 
        $y += $tick_gain / 2;
    }
 
    $x = 0;
    foreach ( @{$graph_src} ) {
        my $f = $_->{f};
        my $gain = $_->{gain};
 
        $img->setpixel( color => 'red',
            x => $x0 + $x, y => $y0 - int(($gain * $tick_gain) + 0.5) );
        $x++;
    }
 
    return $img;
}
 
sub calc_gain {
    my ( $w, $params ) = @_;
 
    # H(z) = (b0 + b1 * z^-1 + b2 * z^-2) / (1 + a1 * z^-1 + a2 * z^-2)
    # z^1  = e^(jw)  = cos(w) + j*sin(w)
    # z^-1 = e^(-jw) = cos(w) - j*sin(w)
 
    # H(jw) = {(A-jB) * (C+jD)} / {(C-jD) * (C+jD)} = {(AC + BD) + j(AD - BC)} / (C^2 + D^2)
    # |H(jw)| = sqrt( (AC + BD)^2 + (AD - BC)^2 ) / (C^2 + D^2)
 
    my ( $b0, $b1, $b2, $a1, $a2 ) = map { $params->{$_}; } qw(b0 b1 b2 a1 a2);
    my $sin_w = sin( 2.0 * pi * $w );
    my $sin_2w = sin( 2.0 * pi * 2.0 * $w );
    my $cos_w = cos( 2.0 * pi * $w );
    my $cos_2w = cos( 2.0 * pi * 2.0 * $w );
 
    my $largeA = $b0 + ($b1 * $cos_w) + ($b2 * $cos_2w);
    my $largeB = ($b1 * $sin_w) + ($b2 * $sin_2w);
    my $largeC = 1.0 + ($a1 * $cos_w) + ($a2 * $cos_2w);
    my $largeD = ($a1 * $sin_w) + ($a2 * $sin_2w);
 
    my $AC_plus_BD = ($largeA * $largeC) + ($largeB * $largeD);
    my $AD_minus_BC = ($largeA * $largeD) - ($largeB * $largeC);
    my $d = ($largeC * $largeC) + ($largeD * $largeD);
 
    my $gain = sqrt(($AC_plus_BD * $AC_plus_BD) + ($AD_minus_BC * $AD_minus_BC)) / $d;
 
    return $gain;
}

OSによって、フォントの指定方法が違うみたいのがあって、
これをサンプルとして入れるのは諦めた。
その代わり、振幅特性を数字で出力するスクリプトを同梱することにした。

という訳で、Win32の人はImager::Fontを参考に、
$fontの初期化方法を書き直して貰うとして、
Macの人はこちらの記事を見て貰うとして、
あとは分かんないです、ごめんなさい。

ちなみに、自分の環境だと、こんな感じのグラフが出力される。
20141024

2014/10/28 追記
今日時点でのCassisで動作するようにコードを変更。

おしまい。

Leave a Comment