Сравнение постов

Различия между постами #162878 (31.01.2021 14:42) и #276991 (27.08.2024 23:38).
1
import std.stdio;
2
3
extern(C) __gshared string[] rt_options =
4
    [ "gcopt=minPoolSize:2K maxPoolSize:1M incPoolSize:4K" ];
5
6
auto fastAbs(float x) @system
7
{
8
	uint p = *(cast(uint*) &x);
9
	p &=0x7fffffff;
10
	return *(cast(float*) &p);
11
}
12
13
auto renormalize(Range)(Range r)
14
{
15
	import std.algorithm;
16
17
	auto maximalAbsoluteValue = r
18
								.map!(a => fastAbs(a))
19
								.fold!max;
20
	return r
21
			.map!(a => a / maximalAbsoluteValue);
22
}
23
24
void main()
25
{
26
	import std.algorithm;
27
	import std.complex;
28
	import std.math;
29
	import std.numeric;
30
	import std.random;
31
	import std.range;
32
33
	import ppmformats;
34
35
	auto rng = Random(unpredictableSeed);
36
37
	auto signalWithNoise(float x)
38
	{
39
		return (3.3 * sin(2*x)) + uniform(-0.3, 0.3, rng);
40
	}
41
	
42
	alias squareWave = delegate(float x) {
43
	  return
44
          ((4 * 10) / (PI)) * (sin(x) + (sin(3 * x) / (3 * x)) + (sin(5 * x) /(5 * x)) + (sin(7 * x) / (7 * x)) + (sin(9 * x) / (9 * x)) + (sin(11 * x) / (11 * x))) + (sin(13 * x) / (13 * x)) + (sin(15 * x) / (15 * x)) + (sin(17 * x) / (17 * x));
45
	};
46
47
	auto signal = iota(0, 2_048)
48
						.map!squareWave;
49
50
	auto normalizedFFT(Range)(Range r)
51
	{
52
		return 
53
				r
54
				  .fft
55
				  .map!(a => 20.0f * log10(abs(a ^^ 2)))
56
				  .renormalize;
57
	}
58
59
	auto pd = signal
60
					.slide(1_024)
61
					.map!(a => normalizedFFT(a));
62
63
	auto img = new P6Image(1024, 1024);
64
65
	foreach (i, e; pd.enumerate)
66
	{
67
		foreach (j, s; e.enumerate)
68
		{
69
			img[i, j] = new RGBColor(
70
				cast(int) (0.8 * 255.0 * abs(s)),
71
				0,
72
				255 - cast(int) (255 * abs(s))
73
			);
74
		}
75
	}
76
	img.save(`fft.ppm`);		
77
}