def spectral_entropy(X_dataA,
                     X_dataB,
                     X_testdataA,
                     y_labelA,
                     y_labelB,
                     if_plot=False):
    """Get the Spectral entropy"""

    entropy_freqA = normalize(
        np.array([
            audioFeatureExtraction.stSpectralEntropy(X, numOfShortBlocks=150)
            for X in X_dataA
        ]))
    entropy_freqB = normalize(
        np.array([
            audioFeatureExtraction.stSpectralEntropy(X, numOfShortBlocks=50)
            for X in X_dataB
        ]))
    entr_freqtestA = normalize(
        np.array([
            audioFeatureExtraction.stSpectralEntropy(X, numOfShortBlocks=150)
            for X in X_testdataA
        ]))

    if if_plot:
        featurePlot(entropy_freqA,
                    'A',
                    y_labelA,
                    title='Spectral Entropy (set A) vs Feature Labels (set A)')
        featurePlot(entropy_freqB,
                    'B',
                    y_labelB,
                    title='Spectral Entropy (set B) vs Feature Labels (set B)')

    return entropy_freqA, entropy_freqB, entr_freqtestA
def signal_energy_frame(x_dataA,
                        x_dataB,
                        x_testdataA,
                        y_labelA,
                        y_labelB,
                        if_plot=False):
    """signal energy of frame"""
    energyA = normalize(
        np.array([audioFeatureExtraction.stEnergy(x) for x in x_dataA]))
    energyB = normalize(
        np.array([audioFeatureExtraction.stEnergy(x) for x in x_dataB]))
    ener_testA = normalize(
        np.array([audioFeatureExtraction.stEnergy(x) for x in x_testdataA]))

    if if_plot:
        featurePlot(
            energyA,
            'A',
            y_labelA,
            title='Signal Energy of Frame (set A) vs Feature Labels (set A)')
        featurePlot(
            energyB,
            'B',
            y_labelB,
            title='Signal Energy of Frame (set B) vs Feature Labels (set B)')

    return energyA, energyB, ener_testA
def zero_crossing(x_dataA,
                  x_dataB,
                  x_testdataA,
                  y_labelA,
                  y_labelB,
                  if_plot=False):
    """zero crossing rate of frame"""
    zero_crossingsA = normalize(
        np.array([audioFeatureExtraction.stZCR(x) for x in x_dataA]))
    zero_crossingsB = normalize(
        np.array([audioFeatureExtraction.stZCR(x) for x in x_dataB]))
    zc_testA = normalize(
        np.array([audioFeatureExtraction.stZCR(x) for x in x_testdataA]))

    if if_plot:
        featurePlot(
            zero_crossingsA,
            'A',
            y_labelA,
            title='Zero Crossings Rate of Frame (set A) vs Feature Labels'
            ' (set A)')
        featurePlot(
            zero_crossingsB,
            'B',
            y_labelB,
            title='Zero Crossings Rate of Frame (set B) vs Feature Labels'
            ' (set B)')

    return zero_crossingsA, zero_crossingsB, zc_testA
def spectral_centroid_frame(X_dataA,
                            X_dataB,
                            X_testdataA,
                            y_labelA,
                            y_labelB,
                            framerate_A,
                            framerate_B,
                            if_plot=False):
    """Get Spectral centroid of frame (given abs(FFT))"""

    FsA = int(framerate_A[0] / 20)  # framerate is the same for all of set A
    FsB = int(framerate_B[0] / 10)  # framerate is the same for all of set B

    centroidA = np.array([
        audioFeatureExtraction.stSpectralCentroidAndSpread(np.abs(X), FsA)
        for X in X_dataA
    ])
    centroidA[:, 0] = normalize(centroidA[:, 0])
    centroidA[:, 1] = normalize(centroidA[:, 1])

    centroidB = np.array([
        audioFeatureExtraction.stSpectralCentroidAndSpread(np.abs(X), FsB)
        for X in X_dataB
    ])
    centroidB[:, 0] = normalize(centroidB[:, 0])
    centroidB[:, 1] = normalize(centroidB[:, 1])

    cent_testA = np.array([
        audioFeatureExtraction.stSpectralCentroidAndSpread(np.abs(X), FsA)
        for X in X_testdataA
    ])
    cent_testA[:, 0] = normalize(cent_testA[:, 0])
    cent_testA[:, 1] = normalize(cent_testA[:, 1])

    if if_plot:
        featurePlot(
            centroidA[:, 0],
            'A',
            y_labelA,
            title='Centroids (1st Column of Set A) vs. Feature Labels (Set A)')
        featurePlot(
            centroidA[:, 1],
            'A',
            y_labelA,
            title='Centroids (2nd Column of Set A) vs. Feature Labels (Set A)')
        featurePlot(
            centroidB[:, 0],
            'B',
            y_labelB,
            title='Centroids (1st Column of Set B) vs. Feature Labels (Set B)')
        featurePlot(
            centroidB[:, 1],
            'B',
            y_labelB,
            title='Centroids (2nd Column of Set B) vs. Feature Labels (Set B)')

    return centroidA, centroidB, cent_testA
def spectral_flux(X_dataA,
                  X_dataB,
                  X_testdataA,
                  y_labelA,
                  y_labelB,
                  if_plot=False):
    """ Get spectral flux """
    fluxA = normalize(
        np.array([
            np.abs(
                audioFeatureExtraction.stSpectralFlux(X[:int(len(X) / 2)],
                                                      X[int(len(X) / 2):]))
            for X in X_dataA
        ]))
    fluxB = normalize(
        np.array([
            np.abs(
                audioFeatureExtraction.stSpectralFlux(X[:int(len(X) / 2)],
                                                      X[int(len(X) / 2) + 1:]))
            for X in X_dataB
        ]))
    flux_testA = normalize(
        np.array([
            np.abs(
                audioFeatureExtraction.stSpectralFlux(X[:int(len(X) / 2)],
                                                      X[int(len(X) / 2):]))
            for X in X_testdataA
        ]))

    if if_plot:
        featurePlot(fluxA,
                    'A',
                    y_labelA,
                    title='Spectral Flux (set A) vs Feature Labels (set A)')
        featurePlot(fluxB,
                    'B',
                    y_labelB,
                    title='Spectral Flux (set B) vs Feature Labels (set B)')

    return fluxA, fluxB, flux_testA
def entropy_of_energy(x_dataA,
                      x_dataB,
                      x_testdataA,
                      y_labelA,
                      y_labelB,
                      if_plot=False):
    """Entropy of Energy"""
    entropyA = normalize(
        np.array([
            audioFeatureExtraction.stEnergyEntropy(x, numOfShortBlocks=50)
            for x in x_dataA
        ]))
    entropyB = normalize(
        np.array([
            audioFeatureExtraction.stEnergyEntropy(x, numOfShortBlocks=50)
            for x in x_dataB
        ]))
    entr_testA = normalize(
        np.array([
            audioFeatureExtraction.stEnergyEntropy(x, numOfShortBlocks=50)
            for x in x_testdataA
        ]))

    if if_plot:
        featurePlot(
            entropyA,
            'A',
            y_labelA,
            title='Entropy of Energy (set A) vs Feature Labels (set A)')
        featurePlot(
            entropyB,
            'B',
            y_labelB,
            title='Entropy of Energy (set B) vs Feature Labels (set B)')

    return entropyA, entropyB, entr_testA
def get_mcfcc_feat(x_dataA,
                   x_dataB,
                   x_testdataA,
                   framerate_A,
                   framerate_B,
                   if_plot=False):
    """Get the MFCC features"""

    FsA = int(framerate_A[0] / 10)  # framerate is the same for all of set A
    FsB = int(framerate_B[0] / 4)  # framerate is the same for all of set B

    mfccA = np.array([
        mfcc(np.abs(x), samplerate=FsA, numcep=3, winlen=0.025)
        for x in x_dataA
    ])
    mfccA_feat = np.array([[
        np.average(m[:, 0]),
        np.std(m[:, 0]),
        np.average(m[:, 1]),
        np.std(m[:, 1]),
        np.average(m[:, 2]),
        np.std(m[:, 2])
    ] for m in mfccA])
    for i in range(len(mfccA_feat[0])):
        mfccA_feat[:, i] = np.abs(mfccA_feat[:, i]) / max(
            np.abs(mfccA_feat[:, i]))

    mfccB = np.array([
        mfcc(np.abs(x), samplerate=FsB, numcep=5, winlen=0.01, winstep=0.01)
        for x in x_dataB
    ])
    mfccB_feat = np.array([[
        np.average(m[:, 0]),
        np.std(m[:, 0]),
        np.average(m[:, 1]),
        np.std(m[:, 1]),
        np.average(m[:, 2]),
        np.std(m[:, 2])
    ] for m in mfccB])
    for i in range(len(mfccB_feat[0])):
        mfccB_feat[:, i] = np.abs(mfccB_feat[:, i]) / max(
            np.abs(mfccB_feat[:, i]))

    mfcc_testA = np.array([
        mfcc(np.abs(x), samplerate=FsA, numcep=3, winlen=0.025)
        for x in x_testdataA
    ])
    mfcctestA_feat = np.array([[
        np.average(m[:, 0]),
        np.std(m[:, 0]),
        np.average(m[:, 1]),
        np.std(m[:, 1]),
        np.average(m[:, 2]),
        np.std(m[:, 2])
    ] for m in mfcc_testA])
    for i in range(len(mfccA_feat[0])):
        mfccA_feat[:, i] = np.abs(mfccA_feat[:, i]) / max(
            np.abs(mfccA_feat[:, i]))

    if if_plot:
        featurePlot(
            mfccA_feat[:, 0],
            'A',
            title=
            'MFCC Features (1st column) (Set A) vs Features Labels (Set A)')
        featurePlot(
            mfccA_feat[:, 1],
            'A',
            title=
            'MFCC Features (2nd column) (Set A) vs Features Labels (Set A)')
        featurePlot(
            mfccB_feat[:, 0],
            'B',
            title=
            'MFCC Features (1st column) (Set B) vs Features Labels (Set B)')
        featurePlot(
            mfccB_feat[:, 1],
            'B',
            title=
            'MFCC Features (2nd column) (Set B) vs Features Labels (Set B)')

    return mfccA_feat, mfccB_feat, mfcctestA_feat
def set_A(x_dataA, x_testdataA, y_labelA, if_plot=False):
    """Get peaks for A"""
    dataA_peaks = find_peaks(x_dataA, 'A')
    testdataA_peaks = find_peaks(x_testdataA, 'A')

    # Get the S1 and S2 bounds for set A
    s1_boundsA, s2_boundsA = get_S1S2_bounds(x_dataA, dataA_peaks, 'A')
    s1_boundstA, s2_boundstA = get_S1S2_bounds(x_testdataA, testdataA_peaks,
                                               'A')

    # Standard deviation of S1
    stdS1_A = normalize(stdInterval(s1_boundsA, 0, s1_boundsA, 1, x_dataA))
    stdS1_testA = normalize(
        stdInterval(s1_boundstA, 0, s1_boundstA, 1, x_testdataA))

    # Standard deviation of S2
    stdS2_A = normalize(stdInterval(s2_boundsA, 0, s2_boundsA, 1, x_dataA))
    stdS2_testA = normalize(
        stdInterval(s2_boundstA, 0, s2_boundstA, 1, x_testdataA))

    # frequency intervals of S1 and S2
    freqS1_A = freqInterval(x_dataA, s1_boundsA, 0, s1_boundsA, 1)
    freqS2_A = freqInterval(x_dataA, s2_boundsA, 0, s2_boundsA, 1)

    # standard deviation of S1 and S2 frequencies
    stdS1_freqA = normalize(np.array([np.std(f) for f in freqS1_A]))
    stdS2_freqA = normalize(np.array([np.std(f) for f in freqS2_A]))

    # mean of S1 and S2 frequencies
    meanS1_freqA = normalize(np.array([np.average(f) for f in freqS1_A]))
    meanS2_freqA = normalize(np.array([np.average(f) for f in freqS2_A]))

    if if_plot:
        featurePlot(
            stdS1_A,
            'A',
            y_labelA,
            title='Standard Deviation of S1 (set A) vs. Feature Labels (set A)'
        )
        featurePlot(
            stdS2_A,
            'A',
            y_labelA,
            title='Standard Deviation of S2 (set A) vs. Feature Labels (set A)'
        )

        featurePlot(
            stdS1_freqA,
            'A',
            y_labelA,
            title=
            'Standard Deviation of Frequency of S1 (set A) vs. Feature Labels'
            ' (set A)')
        featurePlot(
            meanS1_freqA,
            'A',
            y_labelA,
            title='Mean of Frequency of S1 (set A) vs. Feature Labels (set A)')
        featurePlot(
            stdS2_freqA,
            'A',
            y_labelA,
            title=
            'Standard Deviation of Frequency of S2 (set A) vs. Feature Labels'
            ' (set A)')
        featurePlot(
            meanS2_freqA,
            'A',
            y_labelA,
            title='Mean of Frequency of S2 (set A) vs. Feature Labels (set A)')

        n = 2
        plt.figure()
        plt.title(y_labelA[n])
        plt.plot(x_dataA[n], 'b')
        plt.scatter(dataA_peaks[n], x_dataA[n][dataA_peaks[n]], c='r')
        # plt.show()
        plt.savefig(y_labelA[n] + '.png')

    return stdS1_A, stdS1_testA, stdS2_A, stdS2_testA, meanS1_freqA, meanS2_freqA, stdS1_freqA, stdS2_freqA
def set_B(x_dataB, y_labelB, if_plot=False):
    """get peaks for B"""
    dataB_peaks = find_peaks(x_dataB, 'B')

    # Get S1 and S2 bounds for set B
    s1_boundsB, s2_boundsB = get_S1S2_bounds(x_dataB, dataB_peaks, 'B')

    # Standard deviation of S1
    stdS1_B = normalize(stdInterval(s1_boundsB, 0, s1_boundsB, 1, x_dataB))

    # Standard deviation of S2
    stdS2_B = normalize(stdInterval(s2_boundsB, 0, s2_boundsB, 1, x_dataB))

    # frequency intervals of S1 and S2
    freqS1_B = freqInterval(x_dataB, s1_boundsB, 0, s1_boundsB, 1)
    freqS2_B = freqInterval(x_dataB, s2_boundsB, 0, s2_boundsB, 1)

    # standard deviation of S1 and S2 frequencies
    stdS1_freqB = normalize(np.array([np.std(f) for f in freqS1_B]))
    stdS2_freqB = normalize(np.array([np.std(f) for f in freqS2_B]))

    # mean of S1 and S2 frequencies
    meanS1_freqB = normalize(np.array([np.average(f) for f in freqS1_B]))
    meanS2_freqB = normalize(np.array([np.average(f) for f in freqS2_B]))

    if if_plot:
        featurePlot(
            stdS1_B,
            'B',
            y_labelB,
            title='Standard Deviation of S1 (set B) vs. Feature Labels (set B)'
        )
        featurePlot(
            stdS2_B,
            'B',
            y_labelB,
            title='Standard Deviation of S2 (set B) vs. Feature Labels (set B)'
        )

        featurePlot(
            stdS1_freqB,
            'B',
            y_labelB,
            title=
            'Standard Deviation of Frequency of S1 (set B) vs. Feature Labels'
            ' (set B)')
        featurePlot(
            stdS2_freqB,
            'B',
            y_labelB,
            title=
            'Standard Deviation of Frequency of S2 (set B) vs. Feature Labels'
            ' (set B)')
        featurePlot(
            meanS1_freqB,
            'B',
            y_labelB,
            title='Mean of Frequency of S1 (set B) vs. Feature Labels (set B)')
        featurePlot(
            meanS2_freqB,
            'B',
            y_labelB,
            title='Mean of Frequency of S2 (set B) vs. Feature Labels (set B)')

        n = 390
        plt.figure()
        plt.title(y_labelB[n])
        plt.plot(x_dataB[n], 'b')
        plt.scatter(dataB_peaks[n], x_dataB[n][dataB_peaks[n]], c='r')
        plt.show()

    return stdS1_B, stdS2_B, meanS1_freqB, meanS2_freqB, stdS1_freqB, stdS2_freqB