def average_measurements(input_dir=None, output_dir=None): if input_dir is None: raise TypeError('Input directory path is required!') if output_dir is None: output_dir = os.path.abspath(input_dir) input_dir = os.path.abspath(input_dir) output_dir = os.path.abspath(output_dir) models = {} for file_path in glob(os.path.join(input_dir, '*')): model = os.path.split(file_path)[-1] if not re.search(MOD_REGEX, model, re.IGNORECASE): continue norm = re.sub(MOD_REGEX, '', model, 0, flags=re.IGNORECASE) try: models[norm].append(model) except KeyError as err: models[norm] = [model] for norm, origs in models.items(): if len(origs) > 1: f = FrequencyResponse.generate_frequencies() avg = np.zeros(len(f)) for model in origs: fr = FrequencyResponse.read_from_csv(os.path.join(input_dir, model, model + '.csv')) fr.interpolate() fr.center() avg += fr.raw avg /= len(origs) fr = FrequencyResponse(name=norm, frequency=f, raw=avg) d = os.path.join(output_dir, norm) os.makedirs(d, exist_ok=True) file_path = os.path.join(d, norm + '.csv') fr.write_to_csv(file_path)
def main(): models = {} for file_path in glob(os.path.join(DIR, '*')): model = os.path.split(file_path)[-1] if not (re.search(' sample [a-zA-Z0-9]$', model, re.IGNORECASE) or re.search(' sn[a-zA-Z0-9]+$', model, re.IGNORECASE)): # Skip measurements with sample or serial number, those have averaged results continue norm = re.sub(' sample [a-zA-Z0-9]$', '', model, 0, re.IGNORECASE) norm = re.sub(' sn[a-zA-Z0-9]+$', '', norm, 0, re.IGNORECASE) try: models[norm].append(model) except KeyError as err: models[norm] = [model] for norm, origs in models.items(): if len(origs) > 1: print(norm, origs) avg = np.zeros(613) f = FrequencyResponse.generate_frequencies() for model in origs: fr = FrequencyResponse.read_from_csv( os.path.join(DIR, model, model + '.csv')) fr.interpolate() fr.center() avg += fr.raw avg /= len(origs) fr = FrequencyResponse(name=norm, frequency=f, raw=avg) d = os.path.join(OUT_DIR, norm) if not os.path.isdir(d): os.makedirs(d) fr.write_to_csv(os.path.join(d, norm + '.csv'))
def main(): fig, ax = plt.subplots() diffs = [] # Calculate differences for all models for file in glob(os.path.join('compensation', 'compensated', '**', '*.csv'), recursive=True): file = os.path.abspath(file) comp = FrequencyResponse.read_from_csv(file) comp.interpolate() comp.center() raw_data_path = file.replace('compensated', 'raw') raw = FrequencyResponse.read_from_csv(raw_data_path) raw.interpolate() raw.center() diff = FrequencyResponse(name=comp.name, frequency=comp.frequency, raw=raw.raw - comp.raw) plt.plot(diff.frequency, diff.raw) diffs.append(diff.raw) # Average and smoothen difference f = FrequencyResponse.generate_frequencies() diffs = np.vstack(diffs) diff = np.mean(diffs, axis=0) diff = FrequencyResponse(name='Headphone.com Compensation', frequency=f, raw=diff) diff.smoothen_fractional_octave(window_size=1 / 9, iterations=10) diff.raw = diff.smoothed diff.smoothed = np.array([]) plt.xlabel('Frequency (Hz)') plt.semilogx() plt.xlim([20, 20000]) plt.ylabel('Amplitude (dBr)') plt.ylim([-15, 15]) plt.grid(which='major') plt.grid(which='minor') plt.title('Headphone.com Compensation Function') ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:.0f}')) plt.show() diff.write_to_csv('headphonecom_compensation.csv') diff.plot_graph(show=True, f_min=10, f_max=20000, file_path='headphonecom_compensation.png')
def main(input_dir=None, output_dir=None): if input_dir is None: raise TypeError('Input directory path is required!') if output_dir is None: raise TypeError('Output directory path is required!') input_dir = os.path.abspath(input_dir) output_dir = os.path.abspath(output_dir) models = {} for file_path in glob(os.path.join(input_dir, '*')): model = os.path.split(file_path)[-1] if not (re.search(' sample [a-zA-Z0-9]$', model, re.IGNORECASE) or re.search(' sn[a-zA-Z0-9]+$', model, re.IGNORECASE)): continue norm = re.sub(' sample [a-zA-Z0-9]$', '', model, 0, re.IGNORECASE) norm = re.sub(' sn[a-zA-Z0-9]+$', '', norm, 0, re.IGNORECASE) try: models[norm].append(model) except KeyError as err: models[norm] = [model] for norm, origs in models.items(): if len(origs) > 1: print(norm, origs) avg = np.zeros(613) f = FrequencyResponse.generate_frequencies() for model in origs: fr = FrequencyResponse.read_from_csv( os.path.join(input_dir, model, model + '.csv')) fr.interpolate() fr.center() avg += fr.raw avg /= len(origs) fr = FrequencyResponse(name=norm, frequency=f, raw=avg) d = os.path.join(output_dir, norm) if not os.path.isdir(d): os.makedirs(d) fr.write_to_csv(os.path.join(d, norm + '.csv'))
def main(): # Filenames if_files = list( glob(os.path.join('innerfidelity', 'data', '**', '*.csv'), recursive=True)) if_file_names = [os.path.split(os.path.abspath(f))[-1] for f in if_files] normalized_if_files = [normalize(s) for s in if_file_names] hp_files = list( glob(os.path.join('rtings', 'data', '**', '*.csv'), recursive=True)) # Find matching files matching_if_files = [] matching_hp_files = [] for hp_file in hp_files: file_name = os.path.split(os.path.abspath(hp_file))[-1] for i in range(len(normalized_if_files)): if normalized_if_files[i] == normalize(file_name): matching_hp_files.append(hp_file) matching_if_files.append(if_files[i]) # Write mathces to file for manual inspection df = pd.DataFrame( np.array([matching_hp_files, matching_if_files]).transpose()) df.to_csv('matches.csv', index=False, header=False) fig, ax = plt.subplots() diffs = [] # Calculate differences for all models if_compensation = FrequencyResponse.read_from_csv( os.path.join('innerfidelity', 'resources', 'innerfidelity_compensation_2017.csv')) if_compensation.interpolate() hp_compensation = FrequencyResponse.read_from_csv( os.path.join('rtings', 'resources', 'rtings_compensation.csv')) hp_compensation.interpolate() for i in range(len(matching_if_files)): if_fr = FrequencyResponse.read_from_csv(matching_if_files[i]) if_fr.interpolate() if_fr.center() #if_fr.compensate(if_compensation) hp_fr = FrequencyResponse.read_from_csv(matching_hp_files[i]) hp_fr.interpolate() hp_fr.center() #hp_fr.compensate(hp_compensation) #diff = FrequencyResponse(name=if_fr.name, frequency=if_fr.frequency, raw=hp_fr.error - if_fr.error) diff = FrequencyResponse(name=if_fr.name, frequency=if_fr.frequency, raw=hp_fr.raw - if_fr.raw) plt.plot(diff.frequency, diff.raw) diffs.append(diff.raw) # Average and smoothen difference f = FrequencyResponse.generate_frequencies() diffs = np.vstack(diffs) diff = np.mean(diffs, axis=0) std = np.std(diffs, axis=0) diff = FrequencyResponse(name='Rtings Raw to Innerfidelity Raw', frequency=f, raw=diff) #diff.smoothen(window_size=1/7, iterations=10) diff.smoothen_fractional_octave(window_size=1 / 5, iterations=100) diff.raw = diff.smoothed diff.smoothed = np.array([]) plt.xlabel('Frequency (Hz)') plt.semilogx() plt.xlim([20, 20000]) plt.ylabel('Amplitude (dBr)') plt.ylim([-15, 15]) plt.grid(which='major') plt.grid(which='minor') plt.title('Rtings Raw to Innerfidelity Raw') ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:.0f}')) plt.show() fig, ax = diff.plot_graph(f_min=20, f_max=20000, show=False, color=None) ax.fill_between(diff.frequency, diff.raw + std, diff.raw - std, facecolor='lightblue') plt.legend(['Rtings Raw to Innerfidelity Raw', 'Standard Deviation']) plt.ylim([-10, 10]) fig.savefig(os.path.join('calibration', 'rtings_to_innerfidelity.png'), dpi=240) plt.show() diff.write_to_csv( os.path.join('calibration', 'rtings_to_innerfidelity.csv')) diff.raw *= -1 diff.name = 'Innerfidelity Raw to Rtings Raw' fig, ax = diff.plot_graph(f_min=20, f_max=20000, show=False, color=None) ax.fill_between(diff.frequency, diff.raw + std, diff.raw - std, facecolor='lightblue') plt.legend(['Innerfidelity Raw to Rtings Raw', 'Standard Deviation']) plt.ylim([-10, 10]) fig.savefig(os.path.join('calibration', 'innerfidelity_to_rtings.png'), dpi=240) plt.show() diff.write_to_csv( os.path.join('calibration', 'innerfidelity_to_rtings.csv'))