visqol_rs/
patch_similarity_comparator.rs

1use ndarray::Array2 as ImagePatch;
2use serde::Serialize;
3#[derive(Debug, Clone, Serialize)]
4/// Bundles similarity information of a single patch.
5/// The term `Patch` here refers to a single of spectrogram data produced by a PatchCreator)
6pub struct PatchSimilarityResult {
7    /// Means of the individual frequency bands
8    pub freq_band_means: Vec<f64>,
9    /// Standard deviation of the individual frequency bands
10    pub freq_band_stddevs: Vec<f64>,
11    /// Energy of the degraded file per frequency band
12    pub freq_band_deg_energy: Vec<f64>,
13    /// Calculated patch similarity score
14    pub similarity: f64,
15    /// Reference start of patch in seconds
16    pub ref_patch_start_time: f64,
17    /// Reference end of patch in seconds
18    pub ref_patch_end_time: f64,
19    /// Degraded start of patch in seconds
20    pub deg_patch_start_time: f64,
21    /// Degraded end of patch in seconds
22    pub deg_patch_end_time: f64,
23}
24
25impl PatchSimilarityResult {
26    /// Creates a new similarity result, stores mean, std and energy of degraded signal and sets the time information to 0
27    pub fn new(
28        freq_band_means: Vec<f64>,
29        freq_band_stddevs: Vec<f64>,
30        freq_band_deg_energy: Vec<f64>,
31        similarity: f64,
32    ) -> Self {
33        Self {
34            freq_band_means,
35            freq_band_stddevs,
36            freq_band_deg_energy,
37            similarity,
38            ref_patch_start_time: 0.0,
39            ref_patch_end_time: 0.0,
40            deg_patch_start_time: 0.0,
41            deg_patch_end_time: 0.0,
42        }
43    }
44}
45
46impl Default for PatchSimilarityResult {
47    fn default() -> Self {
48        Self {
49            freq_band_means: Vec::<f64>::new(),
50            freq_band_stddevs: Vec::<f64>::new(),
51            freq_band_deg_energy: Vec::<f64>::new(),
52            similarity: 0.0,
53            ref_patch_start_time: 0.0,
54            ref_patch_end_time: 0.0,
55            deg_patch_start_time: 0.0,
56            deg_patch_end_time: 0.0,
57        }
58    }
59}
60
61/// If implemented, this trait allows for computing a similarity score of 2 patches
62pub trait PatchSimilarityComparator {
63    fn measure_patch_similarity(
64        &self,
65        ref_patch: &mut ImagePatch<f64>,
66        deg_patch: &mut ImagePatch<f64>,
67    ) -> PatchSimilarityResult;
68}