UOMOP

[Plagirism Scoring using Librosa's DTW (only time signal)] 본문

Project/Music Plagiarism Project

[Plagirism Scoring using Librosa's DTW (only time signal)]

Happy PinGu 2022. 11. 6. 17:23
def dtw_score(song1, song1_plag_area, song2, song2_plag_area, sr, rand_num) :
     # 나중에 모드를 여러개 정해보자.
     # 만약 random이면 비교대상이 random이고, window면 주기적으로 window를 내는 방식이다.

    song1_ext = np.array(extractor(song1, song1_plag_area, sr = sr))
    song2_ext = np.array(extractor(song2, song2_plag_area, sr = sr))

    #================================song1의 난수 생성================================

    song1_start = int(song1_plag_area[0]) * 600 + int(song1_plag_area[1]) * 60 + int(song1_plag_area[3]) * 10 + int(song1_plag_area[4]) * 1 + int(song1_plag_area[6]) /10
    song1_end   = int(song1_plag_area[10]) * 600 + int(song1_plag_area[11]) * 60 + int(song1_plag_area[13]) * 10 + int(song1_plag_area[14]) * 1 + int(song1_plag_area[16]) /10
    song1_len   = int(len(song1) / sr)
    song1_len_plag = int( song1_end - song1_start ) 

    rand_range = song1_len - song1_len_plag

    song1_rand_saver = random.sample( range(0, rand_range), rand_num )

    cnt_1 = 0
    for i in range(0, rand_num) :

        while ( abs( song1_rand_saver[i] - int(song1_start) )  <=   int(song1_len_plag / 2)) :
            new_rand = random.sample( range(0, rand_range), 1 )
            song1_rand_saver[i] = new_rand[0]
            cnt_1 += 1

    #================================song2의 난수 생성================================

    song2_start = int(song2_plag_area[0]) * 600 + int(song2_plag_area[1]) * 60 + int(song2_plag_area[3]) * 10 + int(song2_plag_area[4]) * 1 + int(song2_plag_area[6]) /10
    song2_end   = int(song2_plag_area[10]) * 600 + int(song2_plag_area[11]) * 60 + int(song2_plag_area[13]) * 10 + int(song2_plag_area[14]) * 1 + int(song2_plag_area[16]) /10
    song2_len   = int(len(song2) / sr)
    song2_len_plag = int( song2_end - song2_start ) 

    rand_range = song2_len - song2_len_plag

    song2_rand_saver = random.sample( range(0, rand_range), rand_num )

    cnt_2 = 0
    for i in range(0, rand_num) :

        while ( abs( song2_rand_saver[i] - int(song2_start) )  <=   int(song2_len_plag / 2)) :
            new_rand = random.sample( range(0, rand_range), 1 )
            song2_rand_saver[i] = new_rand[0]
            cnt_2 += 1


    #================================모든 난수 생성 완료================================
    #================================song1 random data 저장 (2차원 배열로)================================

    song1_rand_data = []

    for i in range(0, rand_num) :
        song1_rand_data.append([])
        for j in range(0, len(song1_ext)) :
            song1_rand_data[i].append(0)

    for i in range(0, rand_num) :
        song1_rand_data[i] = song1[song1_rand_saver[i] * sr : (song1_rand_saver[i] + song1_len_plag) * sr]

    #================================song2 random data 저장 (2차원 배열로)================================
    song2_rand_data = []

    for i in range(0, rand_num) :
        song2_rand_data.append([])
        for j in range(0, len(song2_ext)) :
            song2_rand_data[i].append(0)

    for i in range(0, rand_num) :
        song2_rand_data[i] = song2[song2_rand_saver[i] * sr : (song2_rand_saver[i] + song2_len_plag) * sr]
    # ================================모든 random data 생성 완료================================
    
    # ================================dtw scoring================================

    saver = []
    order = 0

    song1song2 = FastDTW(song1_ext, song2_ext)

    saver.append(song1song2)


    
    for i in range(0, rand_num) :
        saver.append(FastDTW(song1_ext, song2_rand_data[i]))
        saver.append(FastDTW(song2_ext, song1_rand_data[i]))
        
    save_df = pd.DataFrame(saver, columns = ['distance'])

    save_df = save_df.sort_values(by = 'distance')
    index_saver = save_df.index

    for i in range(0, len(index_saver)) :
        if(index_saver[i] != 0) :
            order += 1

        else :
            order += 1

            break

    score = round(100 - ((order / (2 * rand_num + 1)) * 100), 2)
    
    return score

 

 

 

 

 

Comments