import numpy as np
import matplotlib.pyplot as plt # for visualize
import scipy.signal as signal
import wavio
import logging
import os
import subprocess
import argparse
parser = argparse.ArgumentParser(description="Run restore test")
parser.add_argument("-T", "--testtype", choices=["float", "fixed"], default="float",
help="Select test type: float (default) or fixed")
args = parser.parse_args()
audio_out_path = "../output"
embed_binary_path = "../../build/test/embed/test_embed"
if args.testtype == "fixed":
lfe_restore_binary_path="../../build/test_lfe_fast_restore_fixed"
audio_out_path = "../output_fixed"
logging.basicConfig(
level=logging.ERROR,
format='%(message)s',
filename='psnr_fixed.csv',
filemode='w'
)
else:
lfe_restore_binary_path="../../build/test_lfe_fast_restore"
audio_out_path = "../output_float"
logging.basicConfig(
level=logging.ERROR,
format='%(message)s',
filename='psnr.csv',
filemode='w'
)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(logging.Formatter('%(asctime)s : %(message)s'))
logging.getLogger().addHandler(console_handler)
def PSNR(origin, ref, Qmax):
or_i=(origin*Qmax).astype(int)
ref_i=(ref*Qmax).astype(int)
scope=Qmax*2-1
mse=np.mean((or_i-ref_i)**2, axis=0, dtype='float64')
if mse==0:
return np.inf
psnr=10*np.log10(scope**2/mse)
return psnr
# Check if want to plot fft
def plot_fft(data, fs, filename):
df = fs / len(data)
f_step = np.arange(0, len(data)) * df
xax = f_step[0:int(len(data) / 2 + 1)] # x축
y_fft = np.fft.fft(data)
# Draw ir at modified
plt.plot(xax, np.abs(y_fft[0:int(len(data) / 2 + 1)]), label='data magnitude')
plt.savefig(filename)
plt.close()
def plot_stream_result(result):
y = np.zeros(len(result))
for i in range(len(result)):
y[i] = int(result[i][0]) * 4 + int(result[i][1]) * 2 + int(result[i][2])
plt.plot(y)
plt.ylim([-0.5, 8])
def load_cfilter_data(basename, qmax):
mylist=[]
for i in range(4):
data = np.memmap(basename+"_"+str(i)+".pcm", dtype=np.int32, mode='r')
data = data.astype(np.float32)/qmax
mylist.append(data)
return mylist
def load_wav_lfe(file_path):
wav = wavio.read(file_path)
fs=wav.rate
sampwidth=wav.sampwidth
data=wav.data.squeeze()
if fs != 48000:
logging.info ("Not 48k sampling rate")
exit(0)
if len(data.shape) == 1:
lfe = np.asarray(data) # if LFE channel only
else:
if data.shape[1] == 2: # if stereo
logging.info ("Stereo channel")
exit(0)
lfe = np.asarray(data[:, 3]) # Extract LFE
QMax=0
if sampwidth== 2:
lfe = lfe.astype(np.float32)
lfe = lfe / 32768
QMax=32768
elif sampwidth == 3:
lfe = lfe.astype(np.float32)
lfe = lfe / 8388608
QMax=8388608
elif sampwidth ==4: # For test int32
lfe = lfe.astype(np.float32)
lfe = lfe / 2147483647
QMax=2147483647
else:
logging.info ("Support int16/24/32, current sampwidth:", sampwidth)
exit(0)
logging.info(f"{file_path}, sampwidth {sampwidth }")
return lfe, fs, sampwidth, QMax
# Bit check of current original LFE signal
def restore_lfe_signal(lfe,fs,sampwidth, QMax,index,file_path):
f_step = 2000 # step of carrier frequency, in this case : 10k, 12k, 14k
# Test PSNR and signal decoding result
bit_list = ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111','10000']
cnt = 0
for bits in bit_list:
# TV stage
if bits == '0000': # Not IAMF format, then no processing of adder and decoder
lfe_new = lfe
newfile=f"{audio_out_path}/"+str(index)+"_"+bits+".wav"
wavio.write(newfile,lfe_new,fs,sampwidth=sampwidth )
newfile_in=newfile
newfile_out=f"{audio_out_path}/"+str(index)+"_0000_LPF.wav"
cmd_embed=[embed_binary_path,"-i",newfile_in,"-flag",str(16),"-o",newfile_out]
logging.info(cmd_embed)
ret=subprocess.run(cmd_embed)
newfile_in2=newfile_out
newfile_out2=f"{audio_out_path}/"+str(index)+"_0000_LPF_LPF.wav"
cmd_embed=[embed_binary_path,"-i",newfile_in2,"-flag",str(16),"-o",newfile_out2]
logging.info(cmd_embed)
ret=subprocess.run(cmd_embed)
else: # IAMF format
# Add max 4 sine wave to lfe, TV stage
#use the c filter result to add 4 sine
file_path_embed_in=f"{audio_out_path}/"+str(index)+"_0000.wav"
file_path_embed_out=f"{audio_out_path}/"+str(index)+"_"+bits+".wav"
cmd_embed=[embed_binary_path,"-i",file_path_embed_in,"-flag",str(int(bits,2)),"-o",file_path_embed_out]
logging.info(cmd_embed)
ret=subprocess.run(cmd_embed)
#Save file and run test
#file_path_embed_out="../restore/"+str(index)+"_"+bits+".wav"
#sf.write(newfile, lfe_new, fs, subtype)
if bits=='10000':
cmd=[lfe_restore_binary_path, file_path_embed_out , str(int(0))]
else:
cmd=[lfe_restore_binary_path, file_path_embed_out , str(int(bits,base=2))]
ret=subprocess.run(cmd)
if(bits == '0000'):
#refer_file1=newfile2
refer_file1=f"{audio_out_path}/"+str(index)+"_0000_LPF.wav"
c_lfe_lowcut1, restore_fs, rs1, rs2 = load_wav_lfe(refer_file1)
#refer_file2=newfile2.split('.wav')[0]+"_LPF.wav"
refer_file2=f"{audio_out_path}/"+str(index)+"_0000_LPF_LPF.wav"
c_lfe_lowcut2, restore_fs, rs1, rs2 = load_wav_lfe(refer_file2)
logging.info("no iamf layout")
else:
#restore_file=newfile.split('.wav')[0]+"_LPF.wav"
restore_file=file_path_embed_out.split('.wav')[0]+"_LPF.wav"
restore_data, restore_fs, rs1, rs2 = load_wav_lfe(restore_file)
# Recover restore LFE and increase amplitude
cfilter_psnr1=PSNR(c_lfe_lowcut1, restore_data,QMax)
cfilter_psnr2=PSNR(c_lfe_lowcut2, restore_data,QMax)
logging.error('RESULT,%d,%s,%s,%s,%.4f,%.4f'%(index, file_path, bits, restore_file, cfilter_psnr1, cfilter_psnr2))
#print ('RESULT,%d,%s,%s,%s,%.4f,%.4f'%(index, file_path, bits, restore_file, cfilter_psnr1, cfilter_psnr1_delay))
dirpaths=["../../audio/lfe/", "../../audio/wav/"]
if os.path.exists(f"{audio_out_path}"):
os.system(f"rm -rf {audio_out_path}/*")
else:
os.system(f"mkdir {audio_out_path}")
index=1
for dirpath in dirpaths:
for file in os.listdir(dirpath):
if file.endswith(".wav"):
file_path = dirpath+file
logging.info(f"{index}: {file_path}")
lfe,fs,sampwidth,qmax=load_wav_lfe(file_path)
restore_lfe_signal(lfe,fs,sampwidth,qmax, index, file_path)
index=index+1
这段代码输出的log是1: ../../audio/lfe/dolby-leaf-flat-lossless-(www.demolandia.net).wav
../../audio/lfe/dolby-leaf-flat-lossless-(www.demolandia.net).wav, sampwidth 2
target file: ../output_float/1_0000
Sample rate: 48000
Bits per sample: 16
Bit detection pass
['../../build/test/embed/test_embed', '-i', '../output_float/1_0000.wav', '-flag', '1', '-o', '../output_float/1_0001.wav']
input wav: format[1] channels[1] sample_rate[48000] bits_per_sample[16] endianness[little-endian] data_length[5587840]
../output_float/1_0001.wav, sampwidth 2,帮我加一段代码吧,就是把结果写入一个excel,第一列是FIle name,第二列是TC(1、2、3、4、5),第三列是encoded结果,第四列是detected结果,第五列是pass结果(True或者false)
最新发布