[解题报告]541 - Error Correction

本文介绍了一个用于检查布尔矩阵是否具有奇偶性属性,并在必要时通过更改单个位进行修复的算法。该算法适用于各种大小的矩阵,并提供了一种简单有效的错误纠正方法。
  Error Correction 

A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here's a 4 x 4 matrix which has the parity property:

 

1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1

The sums of the rows are 2, 0, 4 and 2. The sums of the columns are 2, 2, 2 and 2.

 


Your job is to write a program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one bit. If this is not possible either, the matrix should be classified as corrupt.

 

Input 

The input file will contain one or more test cases. The first line of each test case contains one integer n(n<100), representing the size of the matrix. On the next n lines, there will be n integers per line. No other integers than 0 and 1 will occur in the matrix. Input will be terminated by a value of 0 for n.

 

Output 

For each matrix in the input file, print one line. If the matrix already has the parity property, print ``OK". If the parity property can be established by changing one bit, print ``Change bit (i,j)" where i is the row and j the column of the bit to be changed. Otherwise, print ``Corrupt".

 

Sample Input 

 

4
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1
4
1 0 1 0
0 0 1 0
1 1 1 1
0 1 0 1
4
1 0 1 0
0 1 1 0
1 1 1 1
0 1 0 1
0

 

Sample Output 

 

OK
Change bit (2,3)
Corrupt

 

 


Miguel A. Revilla 
1999-01-11
 
 
没啥好讲的。。。。
#include<stdio.h>
int ar[101][101];
int main()
{
    int n,i,j;
    while (scanf("%d",&n)!=EOF&&n) 
    {
        for(i=0;i<n;i++)
            for (j=0;j<n;j++)
                scanf("%d",&ar[i][j]);
        int cnt_r=0,cnt_c=0;
        int x=-1,y=-1;
        for (i=0;i<n;i++) 
        {
            int sum=0;
            for(j=0;j<n;j++) sum+=ar[i][j];
            if(sum&1) cnt_r++,x=i;
        }
        for (j=0;j<n;j++) 
        {
            int sum=0;
            for (i=0;i<n;i++) sum+=ar[i][j];
            if (sum&1) cnt_c++,y=j;
        }
        if(x==-1&&y==-1) puts("OK");
        else if(cnt_r==1&&cnt_c==1) printf("Change bit (%d,%d)\n",x+1,y+1);
        else puts("Corrupt");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/TheLaughingMan/archive/2013/02/24/2924708.html

Please implement a simple linear regression model using Python, using gradient descent. The function is defined as def linear_regression_gradient_descent(X, y, learning_rate, iterations), where X is the feature data (two-dimensional array or list), y is the target data (one-dimensional array or list), learning_rate is the learning rate, and iterations is the number of iterations for gradient descent. The return value of the function is a tuple, containing the coefficients and intercept of the model这个是我的问题,以下之我的解决步骤,section_number: 1 section_description: Identify the task of implementing a linear regression model with gradient descent section_content: step1: [思考开始] Alright, I need to implement a simple linear regression model using Python, specifically using gradient descent. The function is defined as def linear_regression_gradient_descent(X, y, learning_rate, iterations), where X is the feature data, which is a two-dimensional array or list, y is the target data, a one-dimensional array or list, learning_rate is the learning rate for gradient descent, and iterations is the number of iterations for gradient descent. The function should return a tuple containing the coefficients and intercept of the model. *** *** section_number: 2 section_description: Understanding of linear regression and gradient descent section_content: step2: First, I need to understand what linear regression is. It's a statistical method that allows us to summarize and study relationships between continuous variables. In this case, we have multiple features since X is a two-dimensional array, which means it's multiple linear regression. step3: In multiple linear regression, the model can be represented as: step4: y = b1*x1 + b2*x2 + ... + bn*xn step5: Where: - y is the dependent variable (target) - x1, x2, ..., xn are the independent variables (features) - b0 is the intercept - b1, b2, ..., bn are the coefficients for each feature step6: I need to find the values of b0, b1, ..., bn that minimize the cost function, which is typically the mean squared error between the predicted values and the actual values. step7: Gradient descent is an optimization algorithm that can be used to find the values of these coefficients and intercept by iteratively updating them in the direction of the steepest descent of the cost function. *** *** section_number: 3 section_description: Plan to implement gradient descent for finding coefficients section_content: step8: So, my task is to implement gradient descent to find the best values for b0 and b1, b2, etc. step9: First, I need to initialize the coefficients and intercept. Usually, we initialize them to zero or some random values. For simplicity, I'll initialize them to zero. step10: Next, I need to define the cost function, which is the mean squared error. step11: Then, I need to compute the gradients of the cost function with respect to each coefficient and the intercept. These gradients will tell me how to adjust the coefficients and intercept to reduce the cost. step12: After computing the gradients, I'll update the coefficients and intercept by subtracting the gradients multiplied by the learning rate. step13: I need to repeat this process for a specified number of iterations. step14: Finally, I'll return the coefficients and intercept as a tuple. *** *** section_number: 4 section_description: Handling of data dimensions and intercept separately section_content: step15: Let me think about how to handle the data. X is a two-dimensional array or list, so it should have multiple features. y is a one-dimensional array or list. step16: I need to make sure that X and y have compatible dimensions. step17: Also, I need to handle the intercept. In multiple linear regression, the intercept can be considered as a coefficient for a feature that is always 1. step18: To make the implementation easier, I can add a column of ones to X for the intercept. step19: Wait, but the problem says to return the intercept separately, not as part of the coefficients. So, I need to keep track of the intercept separately. step20: Alternatively, I can include the intercept in the coefficients by adding a column of ones to X, and then the first coefficient will be the intercept. step21: But the problem asks for a tuple containing the coefficients and intercept, so perhaps it's better to keep them separate. *** *** section_number: 5 section_description: Outline of the implementation steps section_content: step22: Let me think about how to implement this. step23: Initialize the coefficients to a list of zeros, with length equal to the number of features. step24: Initialize the intercept to zero. step25: For each iteration: step26: Compute the predicted values: y_pred = intercept + X.dot(coefficients) step27: Compute the gradients for the intercept and coefficients. step28: The gradient for the intercept is the mean of (y_pred - y) step29: The gradient for each coefficient is the mean of (y_pred - y) * X[:, feature_index] step30: Update the intercept and coefficients: step31: intercept = intercept - learning_rate * gradient_intercept step32: coefficients = coefficients - learning_rate * gradient_coefficients step33: After all iterations, return the coefficients and intercept as a tuple. *** *** section_number: 6 section_description: Considerations for gradient calculations and use of numpy section_content: step34: Wait, I need to make sure about the gradient calculations. step35: In linear regression, the cost function is: step36: J = (1/(2*m)) * sum((y_pred - y)^2) step37: Where m is the number of training examples. step38: The gradients are the partial derivatives of J with respect to each parameter. step39: For the intercept (b0): step40: dJ/db0 = (1/m) * sum(y - y_pred) step41: For each coefficient (b1, b2, ...): step42: dJ/dbj = (1/m) * sum((y_pred - y) * xj) step43: Where xj is the j-th feature. step44: So, I need to compute these gradients and update the parameters accordingly. step45: I need to make sure that the dimensions match when performing these operations. step46: Since X is a two-dimensional array, and y is one-dimensional, I need to ensure that the dot product and multiplications are done correctly. step47: In Python, I can use numpy for numerical operations, but the problem doesn't specify if I can use numpy or if I should implement it using pure Python lists. step48: Looking back at the problem statement, it says "two-dimensional array or list" and "one-dimensional array or list", so I think I should implement it using pure Python lists without relying on numpy. step49: But implementing matrix operations with pure Python lists can be cumbersome and inefficient, especially for large datasets. step50: However, since the problem doesn't specify, I'll assume that I can use numpy for numerical operations, as it's common practice in data science. *** *** section_number: 7 section_description: Code outline and considerations for potential edge cases section_content: step51: So, I'll import numpy at the beginning. step52: Now, I'll outline the steps in code: step53: 1. Import numpy. step54: 2. Define the function linear_regression_gradient_descent(X, y, learning_rate, iterations): step55: a. Convert X and y to numpy arrays for easier numerical operations. step56: b. Initialize coefficients to a list of zeros with length equal to the number of features. step57: c. Initialize intercept to zero. step58: d. Perform gradient descent for the given number of iterations: step59: i. Compute y_pred = intercept + X.dot(coefficients) step60: ii. Compute the error: y_pred - y step61: iii. Compute the gradient for the intercept: (1/m) * np.sum(error) step62: iv. Compute the gradient for coefficients: (1/m) * np.dot(X.T, error) step63: v. Update intercept and coefficients: step64: intercept -= learning_rate * gradient_intercept step65: coefficients -= learning_rate * gradient_coefficients step66: e. After all iterations, return the coefficients and intercept as a tuple. step67: Wait, in step d.iv, np.dot(X.T, error) will give the sum of the product of each feature with the error, which is what we need for the gradients of the coefficients. step68: But I need to make sure that the dimensions are correct. step69: Assuming X is of shape (m, n), where m is the number of samples and n is the number of features. step70: y is of shape (m,). step71: coefficients is of shape (n,). step72: intercept is a scalar. step73: Then, y_pred = intercept + X.dot(coefficients) should be of shape (m,). step74: error = y_pred - y, shape (m,). step75: gradient_intercept = (1/m) * np.sum(error) step76: gradient_coefficients = (1/m) * np.dot(X.T, error), shape (n,) step77: Then, updating coefficients and intercept: step78: intercept = intercept - learning_rate * gradient_intercept step79: coefficients = coefficients - learning_rate * gradient_coefficients step80: This seems correct. step81: Now, I need to consider if there is any need to normalize the features or handle scaling, but since the problem doesn't mention it, I'll assume that the features are already scaled appropriately. step82: Also, I need to make sure that the function returns the coefficients and intercept in a tuple, with coefficients being a list or array, and intercept being a scalar. step83: I should also think about edge cases, such as: step84: - If X has only one feature. step85: - If X has multiple features. step86: - If the number of samples in X and y don't match. step87: - If learning_rate is zero or negative. step88: - If iterations is zero or negative. step89: I need to handle these cases appropriately, perhaps by adding assertions or checks at the beginning of the function. step90: But since the problem doesn't specify handling of invalid inputs, I'll assume that the inputs are valid. step91: Also, I need to make sure that the function is efficient and doesn't have unnecessary computations. step92: Now, I'll proceed to write the code accordingly. [思考结束] *** *** section_number: 8 section_description: Explanation of approach for solving the problem with initialization and gradient descent steps section_content: step93: To solve this problem, we need to implement a simple linear regression model using gradient descent in Python. The task is to find the coefficients and intercept of the regression line that minimizes the mean squared error between the predicted and actual target values. step94: ### Approach step95: 1. **Initialization**: - Convert the input feature data `X` and target data `y` into numpy arrays for efficient numerical operations. - Initialize the coefficients to a list of zeros, with a length equal to the number of features. - Initialize the intercept to zero. step96: 2. **Gradient Descent**: - For a specified number of iterations, perform the following steps: - Compute the predicted target values using the current coefficients and intercept. - Calculate the error between the predicted and actual target values. - Compute the gradients for the intercept and coefficients. - Update the intercept and coefficients using the gradients and the learning rate. step97: 3. **Return the Results**: - After completing the iterations, return the coefficients and intercept as a tuple. *** *** section_number: 9 section_description: Implementation of the solution using numpy for gradient descent section_content: step98: ### Solution Code step99: ```python import numpy as np step100: def linear_regression_gradient_descent(X, y, learning_rate, iterations): # Convert X and y to numpy arrays X = np.array(X) y = np.array(y) # Number of samples and features m, n = X.shape # Initialize coefficients and intercept coefficients = np.zeros(n) intercept = 0.0 # Gradient descent for _ in range(iterations): # Predicted values y_pred = intercept + np.dot(X, coefficients) # Errors error = y_pred - y # Gradients gradient_intercept = (1/m) * np.sum(error) gradient_coefficients = (1/m) * np.dot(error, X) # Update intercept and coefficients intercept -= learning_rate * gradient_intercept coefficients -= learning_rate * gradient_coefficients return (coefficients.tolist(), intercept) ``` *** *** section_number: 10 section_description: Detailed explanation of the implementation and results section_content: step101: ### Explanation step102: - **Initialization**: - We convert `X` and `y` to numpy arrays to leverage numpy's efficient array operations. - The coefficients are initialized to zeros, and the intercept is initialized to zero. step103: - **Gradient Descent**: - For each iteration, we calculate the predicted values (`y_pred`) using the current coefficients and intercept. - The error is the difference between the predicted and actual target values. - The gradients for the intercept and coefficients are computed as the mean of the error and the dot product of the feature matrix transpose and the error, respectively. - The intercept and coefficients are updated by subtracting the learning rate multiplied by their respective gradients. step104: - **Return the Results**: - After completing the specified number of iterations, the function returns the coefficients and intercept as a tuple, converting the coefficients back to a list for compatibility. step105: This approach efficiently finds the optimal coefficients and intercept for the linear regression model using gradient descent.请你根据以下的规则进行标注section_number:准确填写当前标注的 section 的序号。 contain_new_solution:判断当前段落是否开启新的思路尝试,若有则标注 “1”,否则标注 “0” 。若为 “1”,请进一步明确新的思路尝试开始的步骤(填入 new_solution_step )。 idea_correct:判断当前段落内思路是否正确或有用。如果该段落内所采用的思路或方法能在解题中具有实际意义,则标记为 “1”;否则,标记为 “0”。 contain_error:判断当前段落是否存在错误。若存在错误,该字段标记为 “1”,并需继续填写后续错误相关字段;若不存在错误,则标记为 “0”,可跳过错误相关字段,直接进行下一项标注。 error_step:若当前段落存在错误,则需详细记录具体是在哪一步出错。 explanation:用一句话简单描述错误原因。 correction:给出该步骤的正确推理或计算过程。 contain_reflection:判断该段落是否包含正向的反思。如果段落中有对错误或思路的积极反思,则标记为 “1”,否则标记为 “0”。若标记为 “1”,需记录反思从哪个步骤开始(填入 reflection_start_step )请你将上述的10个section,进行标注
05-12
如何检验结果是否合理 import numpy as np import matplotlib.pyplot as plt import pandas as pd import os import pywt from scipy.signal import savgol_filter, find_peaks from scipy.interpolate import interp1d import sys import io import json # 设置中文字体 plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 更通用的中文字体 plt.rcParams['axes.unicode_minus'] = False def create_result_folder(folder_name="results"): if not os.path.exists(folder_name): os.makedirs(folder_name) return folder_name def save_plot(save_path, filename, fig=None): if fig is not None: fig.savefig(os.path.join(save_path, filename), dpi=300, bbox_inches='tight') plt.close(fig) else: plt.savefig(os.path.join(save_path, filename), dpi=300, bbox_inches='tight') plt.close() path = os.path.dirname(os.path.abspath(__file__)) # ✅ 1. 数据读取与异常值剔除 def get_data(file_path): file_name = os.path.basename(file_path) data = pd.read_excel(file_path, header=0) if data.isna().any().any(): print(f"{file_name}存在空值:") print(data.isna().sum()) else: print(f"{file_name}无空值") wavenumber = data.iloc[:, 0].values reflectivity = data.iloc[:, 1].values negative_num = np.sum(reflectivity < 0) over_one_num = np.sum(reflectivity > 100) print(f"{file_name}反射率小于0的个数:{negative_num}") print(f"{file_name}反射率大于100的个数:{over_one_num}") return wavenumber, reflectivity def outlier_removal(wavenumber, reflectivity): valid_value = (reflectivity >= 0) & (reflectivity <= 100) return wavenumber[valid_value], reflectivity[valid_value] # ✅ 2. 数据平滑与去噪 def apply_savgol_filter(reflectivity, window_length=11, polyorder=3): return savgol_filter(reflectivity, window_length=window_length, polyorder=polyorder) def wavelet_denoise(signal, wavelet='db4', level=1, mode_ext='constant'): coeffs = pywt.wavedec(signal, wavelet, level=level, mode=mode_ext) threshold = np.std(coeffs[-level]) * np.sqrt(2 * np.log(len(signal))) coeffs = [pywt.threshold(c, threshold, mode='soft') for c in coeffs] reconstructed = pywt.waverec(coeffs, wavelet, mode=mode_ext) return reconstructed def equal_length(signal, target_length): x = np.linspace(0, 1, len(signal)) x_target = np.linspace(0, 1, target_length) f = interp1d(x, signal, kind='linear') return f(x_target) def normalize(reflectivity): return reflectivity / 100.0 # ✅ 3. 导数法检测波峰波谷 def find_peaks_by_derivative(wavenumber, reflectivity, threshold=1e-3): first_derivative = np.gradient(reflectivity, wavenumber) second_derivative = np.gradient(first_derivative, wavenumber) peak_indices = np.where((np.abs(first_derivative) < threshold) & (second_derivative < 0))[0] return peak_indices def find_valleys_by_derivative(wavenumber, reflectivity, threshold=1e-3): first_derivative = np.gradient(reflectivity, wavenumber) second_derivative = np.gradient(first_derivative, wavenumber) valley_indices = np.where((np.abs(first_derivative) < threshold) & (second_derivative > 0))[0] return valley_indices # ✅ 4. 提取主频信息 def get_dominant_frequency(signal, fs=1.0): n = len(signal) fft_result = np.fft.fft(signal) freqs = np.fft.fftfreq(n, d=1/fs) magnitude = np.abs(fft_result) dominant_freq = freqs[np.argmax(magnitude[1:n//2]) + 1] return abs(dominant_freq) def plot_fft(signal, fs=1.0, title="FFT"): n = len(signal) fft_result = np.fft.fft(signal) freqs = np.fft.fftfreq(n, d=1/fs) magnitude = np.abs(fft_result) fig = plt.figure(figsize=(10, 4)) plt.plot(freqs[:n//2], magnitude[:n//2], label='幅值') plt.title(title, fontsize=14) plt.xlabel('频率(cycles per cm⁻¹)', fontsize=12) plt.ylabel('幅值', fontsize=12) plt.grid(True) plt.legend() plt.tight_layout() plt.close() return fig # ✅ 5. 数据保存函数 def save_peaks_valleys(save_path, wavenumber, reflectivity, peaks, valleys): peaks_data = pd.DataFrame({ '波数': wavenumber[peaks], '反射率': reflectivity[peaks] }) valleys_data = pd.DataFrame({ '波数': wavenumber[valleys], '反射率': reflectivity[valleys] }) peaks_data.to_csv(os.path.join(save_path, '波峰.csv'), index=False) valleys_data.to_csv(os.path.join(save_path, '波谷.csv'), index=False) def save_reflectivity_data(save_path, wavenumber, reflectivity): df = pd.DataFrame({ '波数': wavenumber, '反射率': reflectivity }) df.to_csv(os.path.join(save_path, '去噪后反射率数据.csv'), index=False) def save_dominant_frequency(save_path, freq): with open(os.path.join(save_path, '主频信息.txt'), 'w', encoding='utf-8') as f: f.write(f"信号主频(频域): {freq:.4f} cycles per cm⁻¹\n") def save_data_stats(save_path, file_name, negative_num, over_one_num): with open(os.path.join(save_path, '原始数据统计.txt'), 'w', encoding='utf-8') as f: f.write(f"文件名: {file_name}\n") f.write(f"反射率小于0的个数: {negative_num}\n") f.write(f"反射率大于100的个数: {over_one_num}\n") def save_log(save_path, log_content): with open(os.path.join(save_path, '日志.txt'), 'w', encoding='utf-8') as f: f.write(log_content) # ✅ 6. 新增函数:波数转波长 def wavenumber_to_wavelength(wavenumber): return 1e4 / wavenumber # cm⁻¹ -> μm # ✅ 7. 折射率经验公式 def refractive_index(wavelength): term1 = (0.20075 * wavelength ** 2) / (wavelength ** 2 + 12.07224) term2 = (5.54861 * wavelength ** 2) / (wavelength ** 2 - 0.02641) term3 = (35.65066 * wavelength ** 2) / (wavelength ** 2 - 1268.24708) return np.sqrt(1 + term1 + term2 + term3) def refractive_index_complex(wavelength, reflectance): n0 = refractive_index(wavelength) R = reflectance / 100.0 n_complex = complex(n0, 0) numerator = R * (n0 + 1) ** 2 - (n_complex - 1) ** 2 denominator = 1 - R if denominator <= 0 or np.real(numerator) < 0: kappa = 0.0 else: kappa = np.sqrt(np.real(numerator / denominator)) return complex(n0, kappa) # ✅ 8. 厚度计算 def calculate_thickness(P, wavelength, theta1_deg): theta1 = np.deg2rad(theta1_deg) sin_theta1 = np.sin(theta1) n = refractive_index(wavelength) sqrt_n2_sin2 = np.sqrt(n ** 2 - sin_theta1 ** 2) d = ((P - 0.5) * wavelength * sqrt_n2_sin2) / (2 * n ** 2) return d def calculate_thickness_complex(P, wavelength, theta1_deg, reflectance): theta1 = np.deg2rad(theta1_deg) sin_theta1 = np.sin(theta1) n_complex = refractive_index_complex(wavelength, reflectance) n0 = n_complex.real kappa = n_complex.imag sqrt_n2_sin2 = np.sqrt(n0 ** 2 - sin_theta1 ** 2) absorption_correction = 1.0 + (kappa / n0) ** 2 d = ((P - 0.5) * wavelength * sqrt_n2_sin2) / (2 * n0 ** 2)*absorption_correction return d # ✅ 9. 干涉周期计算 def compute_interference_period_weighted(peaks_wavenumber, valleys_wavenumber): if len(peaks_wavenumber) < 2 or len(valleys_wavenumber) < 2: return None from sklearn.linear_model import RANSACRegressor peak_diffs = np.diff(peaks_wavenumber) valley_diffs = np.diff(valleys_wavenumber) all_diffs = np.concatenate([peak_diffs, valley_diffs]) X = np.arange(len(all_diffs)).reshape(-1, 1) y = all_diffs ransac = RANSACRegressor() ransac.fit(X, y) inlier_mask = ransac.inlier_mask_ P = np.mean(all_diffs[inlier_mask]) return P # ✅ 10. 厚度计算并保存 def compute_and_save_thickness(save_path, peaks_wavenumber, valleys_wavenumber, reflectivity_denoised, theta1_deg): if len(peaks_wavenumber) < 2 or len(valleys_wavenumber) < 2: print("无法计算干涉周期,波峰/波谷数量不足") return None P = compute_interference_period_weighted(peaks_wavenumber, valleys_wavenumber) if P is None: print("无法提取干涉周期") return None all_wavenumbers = np.concatenate([peaks_wavenumber, valleys_wavenumber]) mean_wavenumber = np.mean(all_wavenumbers) wavelength = wavenumber_to_wavelength(mean_wavenumber) mean_reflectance = np.mean(reflectivity_denoised) thickness = calculate_thickness_complex(P, wavelength, theta1_deg, mean_reflectance) result_file = os.path.join(save_path, '厚度计算结果.txt') with open(result_file, 'w', encoding='utf-8') as f: f.write(f"入射角: {theta1_deg}°\n") f.write(f"干涉周期: {P:.4f} cm⁻¹\n") f.write(f"平均波长: {wavelength:.4f} μm\n") f.write(f"平均反射率: {mean_reflectance:.2f}%\n") f.write(f"计算厚度: {thickness:.4f} μm\n") f.write(f"公式:d = (P - 0.5) * λ * sqrt(n₀² - sin²&theta;1) / (2n₀²) * (1 + (κ/n₀)²)\n") print(f"\n厚度计算结果已保存至:{result_file}") print(f"厚度:{thickness:.4f} μm") return thickness # ✅ 11. 厚度一致性分析(新增一致性验证) def thickness_consistency_analysis(thickness_10, thickness_15): avg_thickness = (thickness_10 + thickness_15) / 2 relative_error = abs(thickness_10 - thickness_15) / avg_thickness * 100 print(f"\n【厚度一致性分析】") print(f"附件1厚度估计:{thickness_10:.4f} μm") print(f"附件2厚度估计:{thickness_15:.4f} μm") print(f"相对误差:{relative_error:.2f}%") print(f"散度:{np.std([thickness_10, thickness_15]):.4f} μm") print(f"线性拟合残差:{abs(2 * thickness_10 - thickness_15):.4f} μm") print(f"角度敏感性:{(thickness_15 - thickness_10) / 5:.4f} μm/°") if relative_error < 1: print("✅ 厚度估计稳定,误差 < 1%") else: print("⚠️ 厚度估计不稳定,误差 > 1%,可能存在系统误差") return relative_error # ✅ 12. 局部厚度分布图 def plot_thickness_distribution(save_path, wavenumbers, reflectivity, thickness_values): plt.figure(figsize=(12, 6)) plt.plot(wavenumbers, reflectivity, label='反射率', color='blue') min_len = min(len(wavenumbers), len(thickness_values)) plt.plot(wavenumbers[:min_len], thickness_values[:min_len], label='局部厚度估计', color='red', linestyle='--') plt.title('反射率与局部厚度估计分布') plt.xlabel('波数 (cm⁻¹)') plt.ylabel('反射率 (%) / 厚度 (μm)') plt.legend() plt.grid(True) plt.gca().invert_xaxis() save_plot(save_path, '厚度分布图.png') plt.close() def plot_thickness_comparison(save_path, angles, thicknesses): plt.figure(figsize=(8, 6)) plt.bar([str(a) + '°' for a in angles], thicknesses, color=['blue', 'green'], alpha=0.7) plt.title('不同入射角下的厚度估计') plt.xlabel('入射角') plt.ylabel('厚度 (μm)') plt.grid(True) save_plot(save_path, '不同入射角厚度图.png') plt.close() def plot_interference_period_distribution(save_path, P_values): plt.figure(figsize=(12, 6)) plt.hist(P_values, bins=20, edgecolor='black', alpha=0.7) plt.title('干涉周期 $P$ 分布直方图') plt.xlabel('干涉周期 (cm⁻¹)') plt.ylabel('频次') plt.grid(True) save_plot(save_path, '干涉周期分布图.png') plt.close() # ✅ 主程序(重构顺序,符合解题四步法) if __name__ == "__main__": result_folder = create_result_folder("results") file_paths = [ os.path.join(path, "附件1.xlsx"), os.path.join(path, "附件2.xlsx") ] error_report = {} thickness_results = {} for file in file_paths: log_output = io.StringIO() console = sys.stdout sys.stdout = log_output print(f"\n对于文件:{os.path.basename(file)}") wavenumber, reflectivity = get_data(file) if wavenumber is not None and reflectivity is not None: file_name = os.path.splitext(os.path.basename(file))[0] save_path = os.path.join(result_folder, file_name) create_result_folder(save_path) # ✅ 1. 预处理(去噪+峰谷检测) wavenumber_clean, reflectivity_clean = outlier_removal(wavenumber, reflectivity) reflectivity_smooth = apply_savgol_filter(reflectivity_clean) reflectivity_denoised = wavelet_denoise(reflectivity_smooth, mode_ext='constant') reflectivity_denoised = equal_length(reflectivity_denoised, len(wavenumber_clean)) reflectivity_normalized = normalize(reflectivity_denoised) # ✅ 2. 波峰检测(自动识别极值,判定可用性) peak_indices = find_peaks_by_derivative(wavenumber_clean, reflectivity_denoised, threshold=1e-3) valley_indices = find_valleys_by_derivative(wavenumber_clean, reflectivity_denoised, threshold=1e-3) # ✅ 3. 线性标定(单序列法) P = compute_interference_period_weighted(wavenumber_clean[peak_indices], wavenumber_clean[valley_indices]) if P is None: print("⚠️ 干涉周期计算失败,跳过厚度反演") continue # ✅ 4. 厚度反演 if "附件1" in file_name: theta1_deg = 10 elif "附件2" in file_name: theta1_deg = 15 else: theta1_deg = 0 thickness = compute_and_save_thickness(save_path, wavenumber_clean[peak_indices], wavenumber_clean[valley_indices], reflectivity_denoised, theta1_deg) if thickness is not None: thickness_results[file_name] = thickness # ✅ 局部厚度计算(用于绘图) window_size = 50 step = 10 thickness_values = [] for i in range(0, len(wavenumber_clean) - window_size, step): w = wavenumber_clean[i:i + window_size] r = reflectivity_denoised[i:i + window_size] p = compute_interference_period_weighted(w, r) if p is not None: wl = wavenumber_to_wavelength(np.mean(w)) t = calculate_thickness_complex(p, wl, 10, np.mean(r)) thickness_values.append(t) else: thickness_values.append(np.nan) if len(thickness_values) > 0: min_len = min(len(wavenumber_clean), len(thickness_values)) plot_thickness_distribution( result_folder, wavenumber_clean[:min_len], reflectivity_denoised[:min_len], np.array(thickness_values)[:min_len] ) error_report[file_name] = { "去噪前后信号MSE": np.mean((reflectivity_clean - reflectivity_denoised) ** 2), "波峰检测数量": int(len(peak_indices)), "波谷检测数量": int(len(valley_indices)) } sys.stdout = console save_log(save_path, log_output.getvalue()) # ✅ 一致性验证 with open(os.path.join(result_folder, "误差分析报告.json"), "w", encoding="utf-8") as f: json.dump(error_report, f, ensure_ascii=False, indent=4) if "附件1" in thickness_results and "附件2" in thickness_results: thickness_10 = thickness_results["附件1"] thickness_15 = thickness_results["附件2"] thickness_consistency_analysis(thickness_10, thickness_15) plot_thickness_comparison(result_folder, [10, 15], [thickness_10, thickness_15]) # ✅ 干涉周期分布图 P_values = [] for file in file_paths: file_name = os.path.splitext(os.path.basename(file))[0] save_path = os.path.join(result_folder, file_name) wavenumber, reflectivity = get_data(file) wavenumber_clean, reflectivity_clean = outlier_removal(wavenumber, reflectivity) peak_indices = find_peaks_by_derivative(wavenumber_clean, reflectivity_clean) valley_indices = find_valleys_by_derivative(wavenumber_clean, reflectivity_clean) P = compute_interference_period_weighted(wavenumber_clean[peak_indices], wavenumber_clean[valley_indices]) if P is not None: P_values.append(P) if len(P_values) > 0: plot_interference_period_distribution(result_folder, P_values)
最新发布
09-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值