Maria@36
|
1 # -*- coding: utf-8 -*-
|
Maria@36
|
2 """
|
Maria@36
|
3 Created on Fri Sep 1 19:11:52 2017
|
Maria@36
|
4
|
Maria@36
|
5 @author: mariapanteli
|
Maria@36
|
6 """
|
Maria@36
|
7
|
Maria@36
|
8 import pytest
|
Maria@36
|
9
|
Maria@36
|
10 import numpy as np
|
m@85
|
11 import os
|
Maria@36
|
12
|
Maria@36
|
13 import scripts.PitchBihist as PitchBihist
|
Maria@36
|
14
|
Maria@36
|
15
|
Maria@36
|
16 pbi = PitchBihist.PitchBihist()
|
m@85
|
17 TEST_MELODIA_FILE = os.path.join(os.path.dirname(__file__), os.path.pardir,
|
m@85
|
18 'data', 'sample_dataset', 'Melodia', 'mel_1_2_1.csv')
|
Maria@36
|
19
|
Maria@36
|
20 def test_hz_to_cents():
|
Maria@36
|
21 freq_Hz = np.array([32.703, 65.406, 55, 110])
|
Maria@36
|
22 freq_cents = pbi.hz_to_cents(freq_Hz)
|
Maria@36
|
23 freq_cents_true = np.array([0, 1200, 900, 2100])
|
Maria@36
|
24 assert np.array_equal(freq_cents, freq_cents_true)
|
Maria@36
|
25
|
Maria@36
|
26
|
Maria@36
|
27 def test_wrap_to_octave():
|
Maria@36
|
28 cents = np.array([900, 2100, 1200])
|
Maria@36
|
29 octave_cents = pbi.wrap_to_octave(cents)
|
Maria@36
|
30 octave_cents_true = np.array([900, 900, 0])
|
Maria@36
|
31 assert np.array_equal(octave_cents, octave_cents_true)
|
Maria@36
|
32
|
Maria@36
|
33
|
Maria@36
|
34 def test_get_melody_from_file():
|
m@85
|
35 melodia_file = TEST_MELODIA_FILE
|
Maria@36
|
36 melody = pbi.get_melody_from_file(melodia_file)
|
Maria@37
|
37 assert len(melody) < 12. * pbi.melody_sr
|
Maria@36
|
38
|
Maria@36
|
39
|
Maria@36
|
40 def test_get_melody_matrix():
|
Maria@36
|
41 melody = 440 * np.ones(1000)
|
Maria@36
|
42 melody_matrix = pbi.get_melody_matrix(melody)
|
Maria@36
|
43 n_frames = melody_matrix.shape[1]
|
Maria@36
|
44 assert np.array_equal(melody_matrix[45, :], np.ones(n_frames))
|
Maria@36
|
45
|
Maria@36
|
46
|
Maria@39
|
47 def test_second_frame_decomposition():
|
Maria@39
|
48 norigframes = 100
|
Maria@39
|
49 nframes, win2, hop2 = pbi.second_frame_decomposition(norigframes)
|
Maria@39
|
50 assert nframes == 1
|
Maria@39
|
51
|
Maria@39
|
52
|
Maria@39
|
53 def test_second_frame_decomposition():
|
Maria@39
|
54 norigframes = np.ceil(pbi.melody_sr * 16.)
|
Maria@39
|
55 nframes, win2, hop2 = pbi.second_frame_decomposition(norigframes)
|
Maria@39
|
56 # for 16-sec segment with .5 hop size expect ~16 frames round up
|
Maria@39
|
57 assert nframes == 17
|
Maria@39
|
58
|
Maria@39
|
59
|
Maria@36
|
60 def test_bihist_from_melodia():
|
m@85
|
61 melodia_file = TEST_MELODIA_FILE
|
Maria@36
|
62 bihist = pbi.bihist_from_melodia(melodia_file, secondframedecomp=False)
|
Maria@36
|
63 assert bihist.shape == (60, 60)
|
Maria@36
|
64
|
Maria@36
|
65
|
Maria@36
|
66 def test_bihist_from_melodia_n_frames():
|
m@85
|
67 melodia_file = TEST_MELODIA_FILE
|
Maria@36
|
68 bihist = pbi.bihist_from_melodia(melodia_file, secondframedecomp=True)
|
Maria@36
|
69 dur_sec = 11.5 # duration of first file in metadata.csv is > 11 seconds
|
Maria@36
|
70 n_frames_true = np.round((dur_sec - pbi.win2sec) * 2) # for .5 sec hop size
|
Maria@36
|
71 assert bihist.shape[1] == n_frames_true
|
Maria@36
|
72
|
Maria@36
|
73
|
Maria@36
|
74 def test_bihistogram():
|
Maria@36
|
75 melody = 440 * np.ones(1000)
|
Maria@36
|
76 melody_matrix = pbi.get_melody_matrix(melody)
|
Maria@36
|
77 bihist = pbi.bihistogram(melody_matrix, align=False)
|
Maria@36
|
78 assert np.array_equal(bihist, np.zeros((60, 60)))
|
Maria@36
|
79
|
Maria@36
|
80
|
Maria@36
|
81 def test_bihistogram_values():
|
Maria@36
|
82 melody = np.concatenate([440 * np.ones(500), 32.703 * np.ones(500)])
|
Maria@36
|
83 melody_matrix = pbi.get_melody_matrix(melody)
|
Maria@36
|
84 # melody transitions from A to C (bin 45/60 to bin 0/60)
|
Maria@36
|
85 bihist = pbi.bihistogram(melody_matrix, align=False)
|
Maria@36
|
86 # expect only element [45, 0] to be non-zero
|
Maria@36
|
87 assert bihist[45, 0] > 0 and (np.sum(bihist) - bihist[45, 0]) == 0
|
Maria@36
|
88
|
Maria@36
|
89
|
Maria@37
|
90 def test_bihistogram_align():
|
Maria@37
|
91 melody = np.concatenate([660 * np.ones(250), 440 * np.ones(500)])
|
Maria@37
|
92 melody_matrix = pbi.get_melody_matrix(melody)
|
Maria@37
|
93 # bin of max magnitude = A = 45/60
|
Maria@37
|
94 bihist = pbi.bihistogram(melody_matrix, align=True)
|
Maria@37
|
95 # bihist[20, 45] == 0
|
Maria@37
|
96 # we shift up 45 so rows 20-45 and left 45 so cols 45-45
|
Maria@37
|
97 assert bihist[20-45, 0] == 1
|