Example #1
0
def test_roughness():
    """Test function for the roughness calculation of a audio signal

    Test function for the script "comp_roughness" method with signal array
    as input. The input signals are chosen according to the article "Psychoacoustical
    roughness: implementation of an optimized model" by Daniel and Weber in 1997.
    The figure 3 is used to compare amplitude-modulated signals created according to
    their carrier frequency and modulation frequency to the article results.
    One .png compliance plot is generated.

    Parameters
    ----------
    None

    Outputs
    -------
    None
    """

    # Stimulus generation
    stimulus = signal_test(fc=1000, fmod=70, mdepth=1, fs=44100, d=0.2, dB=60)

    # Roughness calculation
    R = comp_roughness(stimulus, fs=44100, overlap=0)

    # Check compliance
    tst = check_compliance(R)

    assert tst
def validation_roughness(signal):
    """Validation function for the roughness calculation of an audio signal

    Validation function for the Audio_signal class "comp_roughness" method with signal array
    as input. The input signals are chosen according to the article "Psychoacoustical
    roughness: implementation of an optimized model" by Daniel and Weber in 1997.
    The figure 3 is used to compare amplitude-modulated signals created according to
    their carrier frequency and modulation frequency to the article results.
    One .png compliance plot is generated.

    Parameters
    ----------
    None

    Outputs
    -------
    None
    """
    # Stimulus parameters
    duration = 0.2
    fs = 44100
    level = 60
    mdepth = 1

    # Overlapping definition for roughness calculation
    overlap = 0

    # Roughness calculation for each carrier frequency
    R = np.zeros((len(signal["fc"])), dtype=dict)
    for ind_fc in range(len(signal["fc"])):
        stimulus = signal_test(signal["fc"][ind_fc], signal["fmod"], mdepth,
                               fs, duration, level)
        roughness = comp_roughness(stimulus, fs, overlap)
        R[ind_fc] = roughness["values"][0]

    # Check compliance
    tst = check_compliance(R, signal)

    return tst
def comparison_roughness():

    # Parameters definition for signals generation
    fs = 44100
    carrier = np.array([250, 1000, 4000])
    fmod = np.array([
        10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140, 160, 180, 200, 300, 400
    ])
    mdepth = 1
    duration = 0.2
    dB = 60

    # Overlapping definition for roughness calculation
    overlap = 0

    # Initialization
    r_zf = np.zeros((len(carrier), len(fmod)))
    r_dw = np.zeros((len(carrier), len(fmod)))
    r_ps = np.zeros((len(carrier), len(fmod)))
    R = np.zeros((len(carrier), len(fmod)))

    # Each carrier frequency is considered separately
    for ind_fc in range(3):
        # Roughness reference values
        r_zf[ind_fc, :] = ref_zf(carrier[ind_fc], fmod)
        r_dw[ind_fc, :] = ref_dw(carrier[ind_fc], fmod)
        r_ps[ind_fc, :] = ref_ps(carrier[ind_fc], fmod)
        # Roughness calculation for each modulation frequency
        for ind_fmod in range(fmod.size):
            signal = signal_test(carrier[ind_fc], fmod[ind_fmod], mdepth, fs,
                                 duration, dB)
            rtemp = comp_roughness(signal, fs, overlap)
            R[ind_fc, ind_fmod] = rtemp["values"]

    fig, (axs1, axs2, axs3) = plt.subplots(3,
                                           1,
                                           figsize=(6, 6),
                                           constrained_layout=True)

    axs1.plot(fmod[0:12],
              r_zf[0, 0:12],
              linestyle="dotted",
              color="black",
              label="reference")
    axs1.plot(fmod[0:12],
              r_dw[0, 0:12],
              marker="x",
              color="red",
              label="Daniel and Weber")
    axs1.plot(fmod[0:12],
              r_ps[0, 0:12],
              marker="o",
              color="#0069a1",
              label="Psysound")
    axs1.plot(fmod[0:12],
              R[0, 0:12],
              marker="s",
              color="#69c3c5",
              label="mosqito")
    axs1.set(xlim=(0, 170), ylim=(0, 1.1))
    axs1.set_title("Carrier frequency of 250 Hz", fontsize=11)
    axs1.legend(loc="upper right", shadow=True)
    axs1.set_ylabel("Roughness [asper]")
    axs1.set_xlabel("Modulation frequency [Hz]")

    axs2.plot(fmod[0:12], r_zf[1, 0:12], linestyle="dotted", color="black")
    axs2.plot(fmod[0:12], r_dw[1, 0:12], marker="x", color="red")
    axs2.plot(fmod[0:12], r_ps[1, 0:12], marker="o", color="#0069a1")
    axs2.plot(fmod[0:12], R[1, 0:12], marker="s", color="#69c3c5")
    axs2.set(xlim=(0, 170), ylim=(0, 1.1))
    axs2.set_ylabel("Roughness [asper]")
    axs2.set_xlabel("Modulation frequency [Hz]")
    axs2.set_title("Carrier frequency of 1000 Hz", fontsize=11)

    axs3.plot(fmod[0:12], r_zf[2, 0:12], linestyle="dotted", color="black")
    axs3.plot(fmod[0:12], r_dw[2, 0:12], marker="x", color="red")
    axs3.plot(fmod[0:12], r_ps[2, 0:12], marker="o", color="#0069a1")
    axs3.plot(fmod[0:12], R[2, 0:12], marker="s", color="#69c3c5")
    axs3.set(xlim=(0, 170), ylim=(0, 1.1))
    axs3.set_ylabel("Roughness [asper]")
    axs3.set_xlabel("Modulation frequency [Hz]")
    axs3.set_title("Carrier frequency of 4000 Hz", fontsize=11)

    fig.savefig("roughness_implementations_comparison" + ".png", format="png")