1. Introduction
As a fundamental ingredient in numerous food products, the authenticity and safety of wheat flour directly influence public health and consumer confidence [
1]. Among various forms of food adulteration, the illegal addition of borax (sodium borate) remains a persistent concern. Although borax can enhance product texture, improve shelf-life stability, and exert preservative effects, its use as a food additive is strictly prohibited under China’s Food Additive Usage Standards (GB 2760-2024) [
2] and U.S. FDA regulations. Nevertheless, driven by economic incentives and compounded in some cases by environmental contamination, such practices continue to occur. Food safety investigations and case reports have documented the intentional addition of trace amounts of borax to flour-based products and meat items (e.g., noodles and meatballs) to evade routine inspections while achieving mild preservative or textural enhancements [
3,
4].
Toxicological evidence indicates that borax exhibits cumulative toxicity. Acute oral ingestion at doses of approximately 2–3 g may result in severe gastrointestinal irritation, renal impairment, and neurological symptoms. Moreover, prolonged exposure to trace levels may lead to chronic health hazards, including potential organ damage [
3,
5]. Notably, both toxicological findings and food safety investigations suggest that health risks and adulteration intent vary across different concentration ranges, thereby providing a scientific rationale for stratified contamination assessment [
6]. The persistence of sporadic violations underscores the urgent need to develop rapid, reliable, and easily deployable detection technologies suitable for routine monitoring and on-site screening [
7].
Traditional borax detection methods—including titration, spectrophotometry, ion chromatography, and other laboratory chemical assays—offer high sensitivity and accuracy but typically require destructive sampling, specialized operators, and time-consuming sample preparation [
8,
9,
10,
11]. These limitations severely hinder high-throughput monitoring, making frequent testing in production lines, warehouses, and market surveillance scenarios difficult to implement [
12]. Consequently, non-destructive testing (NDT) methods are gaining increasing attention, as this technology enables rapid, low-cost, and minimally invasive assessment of flour adulteration without complex pretreatment [
13].
Microwave non-destructive testing (MNDT) has emerged as a promising solution for food quality assessment due to its penetration capability and sensitivity to material dielectric properties [
14]. Changes in moisture, ion content, and chemical composition alter dielectric constants and loss factors, thereby influencing microwave transmission, reflection, and resonance responses [
15]. For powdered foods like wheat flour, borax adulteration alters electromagnetic responses by modifying ionic conductivity and bound water interactions, providing measurable physical detection evidence [
16]. Compared to optical spectroscopy, microwave sensing exhibits lower sensitivity to surface scattering and offers complementary information related to bulk physicochemical properties, making it highly attractive for rapid screening applications [
17]. Unlike laboratory-grade vector network analyzers (VNAs), which are costly and designed for broad-spectrum analysis, the microwave sensing system developed in this study targets a specific frequency range relevant to food adulteration detection, enabling a simpler architecture and lower cost while maintaining sufficient sensitivity for screening.
Despite these advantages, practical deployment of MNDT for adulteration control faces two methodological challenges. First, microwave measurements often yield high-dimensional feature representations (e.g., multi-frequency amplitude/phase responses), while available datasets can be relatively small due to sample preparation constraints and measurement costs [
18]. Second, the regulatory and industrial need is frequently not limited to a binary decision (adulterated vs. non-adulterated) but extends to identifying contamination ranges that reflect risk levels and enforcement thresholds [
19]. In this context, semi-quantitative detection—estimating adulteration severity in discrete intervals—can provide actionable information for decision-making while remaining robust to measurement variability and sample heterogeneity [
20].
Machine learning (ML) offers a data-driven framework to model the complex and potentially nonlinear relationship between MNDT features and adulteration status [
21]. Support vector machines (SVM) and back-propagation (BP) neural networks have been widely applied in chemometrics and sensor-based food analysis, yet their performance may degrade when the feature space is large relative to the number of samples or when hyperparameter tuning is inadequate. Random forests (RF), as an ensemble of decision trees with built-in feature subsampling and bagging, often exhibit strong generalization on small-sample, high-dimensional problems and provide a degree of robustness to noise and correlated predictors [
22]. However, RF performance can still depend on the choice of feature subset and key hyperparameters (e.g., number of trees, leaf size), motivating the use of optimization strategies to improve accuracy and stability [
23].
Metaheuristic optimization algorithms are increasingly used to select informative features and tune model hyperparameters in food sensing studies [
24]. Among them, the whale optimization algorithm (WOA) is a population-based method inspired by the bubble-net feeding behavior of humpback whales, offering a simple structure and competitive global search capability [
25]. By jointly optimizing feature selection and RF hyperparameters, the WOA has the potential to enhance discriminative performance while controlling model complexity [
26]. To provide a meaningful benchmark, variable combination population analysis (VCPA) can be used as a representative feature-selection approach in high-dimensional settings, allowing for a direct comparison of optimization-driven feature selection strategies [
27].
Against this backdrop, this study aims to develop a rapid, non-destructive method for detecting borax adulteration in wheat flour by integrating microwave non-destructive testing technology with machine learning techniques. By capturing broadband microwave transmission responses—including amplitude attenuation spectra and dimensional phase offset spectra—from samples, the method identifies dielectric property changes induced by borax addition. To meet practical food safety testing requirements—specifically detecting trace amounts of borax adulteration—adulterated samples were categorized into three semi-quantitative levels (0%, 0.1–0.9%, and 1–5%). This classification system accurately reflects common scenarios of illegal adulteration in regulatory monitoring and aligns with most countries’ zero-tolerance policies for borax contamination in food [
28].
Given the high dimensionality of microwave features and the imbalance among contamination classes, a hybrid random forest–whale optimization algorithm (RF–WOA) was employed to jointly optimize feature selection and model hyperparameters, with the aim of improving model robustness and parsimony. Specifically, this study (1) develops a broadband microwave sensing system for non-destructive acquisition of amplitude attenuation and phase shift responses, (2) jointly exploits multi-frequency amplitude and phase information for semi-quantitative modeling, (3) introduces an RF–WOA framework for feature selection and hyperparameter optimization under class imbalance, and (4) enables graded assessment of borax adulteration severity to support food safety screening and regulatory inspection.
2. Materials and Methods
2.1. Sample Preparation and Acquisition
The wheat flour was provided by Wu deli Flour Group Co., Ltd. (Handan, China). According to the manufacturer’s nutritional label, the composition per 100 g is energy 1463 kJ, protein 9.6 g, fat 1.6 g, carbohydrates 73.0 g, and sodium 0 mg, complying with the Chinese national standard for wheat flour (GB/T 1355-2021) [
29]. Borax (sodium tetraborate) was purchased from Sinopharm Chemical Reagent Co., Ltd. (Shanghai, China) and met the requirements of GB/T 632-2008 [
30].
Experimental samples prepared by mixing borax with wheat flour were portioned into 50-mL centrifuge tubes to form dry solid mixtures based on mass ratios. Wheat flour and borax powder were added to each tube according to target mass fractions, then mixed for one minute using a centrifugal homogenizer (Super MiniMax 10k, ANPRO Biotechnology Co., Ltd., Chengdu, China). To enhance uniformity and prevent overheating, the mixing operation was repeated three times with 30-s intervals between each cycle. To ensure samples remained in a dry powder state throughout, all processing and measurements were conducted under controlled laboratory conditions (temperature 23 ± 1 °C, relative humidity 50 ± 5%). A total of 155 samples were prepared, each contained in a dedicated testing container. Based on actual illicit usage scenarios, adulteration levels were categorized into three groups: no adulteration (0%, n = 13), low-level adulteration (0.1–0.9%, n = 41), and high-level adulteration (1–5%, n = 101).
2.2. Microwave Sensing System and Frequency-Response Acquisition
The proprietary microwave sensing system shown in
Figure 1 acquires the frequency-domain electromagnetic response of wheat flour samples across a wide bandwidth range. Controlled by an STM32F407VGT6 microcontroller (STMicroelectronics, Geneva, Switzerland), the system incorporates two microwave sources. One source generates a swept signal from 2.5 to 11.5 GHz with 10 MHz steps (covering 900 frequency points), while the other source provides a 1 GHz continuous-wave reference signal. The microwave platform is illustrated in
Figure 2A.
During measurement, the swept signal was transmitted through the flour sample using an antenna-based transmission configuration. As microwaves propagate through heterogeneous materials, the transmitted signal undergoes multiple effects, primarily amplitude attenuation and phase shift, which are governed by the dielectric properties and are influenced by material composition, density, and microstructure. Because wheat flour and borax differ in physicochemical properties, borax adulteration produces distinguishable patterns of attenuation and phase shift across the 2.5–11.5 GHz band.
To extract the sample-induced response, the transmitted swept signal was mixed with the fixed-frequency reference signal and compared against a baseline signal acquired under empty (no-sample) conditions. The resulting amplitude and phase information was converted into voltage outputs by an amplitude/phase detector chip. The voltage signals were digitized by an analog-to-digital converter (ADC) and sent to the STM32 microcontroller, then transferred via serial communication to a host computer for visualization (imaging), storage, and subsequent analysis.
For each of the 155 samples, the system produced two 900-dimensional vectors: an amplitude attenuation spectrum and a phase shift spectrum. These were concatenated at the data level to form a single 1800-dimensional feature vector for modeling. To mitigate the effects of instrument noise, temperature fluctuations, and operator-related variability, the raw microwave sequences were preprocessed using a window size 5 least-squares smoothing method, as shown in
Figure 2B. This preprocessing was applied to both the attenuation and phase sequences prior to multivariate modeling. After filtering, the two modalities were concatenated to obtain the final 1800-dimensional feature vector per sample (with a detection cycle of 60 s).
2.3. Chemical Method Reference Validation Analysis
To verify the presence of borax adulteration in prepared wheat flour samples, a chemical validation method based on curcumin UV-visible spectrophotometry was employed. This chemical analysis was used to obtain borax adulteration results in wheat flour and provide a reference basis for evaluating the microwave detection method. The chemical analysis procedure is detailed as follows: Take 1.000 g of the selected flour sample, add 20.0 mL of distilled water, mix with a vortex mixer for 1 min, then shake at room temperature for 10 min. Centrifuge the suspension at 5000 rpm for 10 min and collect the supernatant for analysis. Color development step: Mix 2.0 mL of the extract with 2.0 mL of 0.1% curcumin ethanol solution and incubate at 50 °C for 10 min. After cooling to room temperature, measure the absorbance at 540 nm using a UV-Vis spectrophotometer (UV-2600, Shimadzu, Kyoto, Japan) with a 1 cm path length. Nine representative samples containing known concentrations of borax were selected for chemical analysis. Each sample underwent three replicate tests, with the average value serving as the borax concentration determined by the approved chemical method. Based on the contamination levels defined in this study, chemically measured concentrations were further categorized into three groups: no adulteration (0%), low-level adulteration (0.1–0.9%), and high-level adulteration (1–5%). These chemical classifications served as reference labels for evaluating the microwave non-destructive testing model.
2.4. Dataset Construction and Sample Partitioning
Based on toxicological thresholds and documented illicit usage patterns, this study categorizes samples into three contamination tiers: 0% (no addition), 0.1–0.9% (low-level adulteration, simulating covert and difficult-to-detect illegal use), and 1–5% (high-level adulteration, simulating deliberate excessive addition). This stratified design enables the model to assess sensitivity to micro-level hazards posing significant health risks while maintaining robustness under overt illegal adulteration scenarios, thereby enhancing the practical relevance of the proposed detection framework.
Given the substantial class imbalance (13/41/101), a repeated stratified random sub-sampling validation strategy was adopted to obtain robust and unbiased performance estimates while strictly preserving the original class distribution. Specifically, the dataset was first stratified according to class labels, and random sampling was then independently conducted within each stratum to partition the samples into a training subset and a test subset, with approximately 70% of samples allocated for training and the remaining 30% reserved for testing, thereby ensuring consistent class proportions across subsets. In each repetition, all procedures including model training, feature selection, and hyperparameter optimization were performed exclusively within the training subset, while the test subset was strictly reserved for final performance evaluation, effectively preventing any information leakage. This entire stratification–sampling–training–testing process was repeated 20 times using different random seeds to comprehensively assess model stability and to quantify the performance variability induced by data partitioning.
2.5. Semi-Quantitative Modeling Strategy
To avoid potential interference from wheat flour composition and quality in borax adulteration detection, this study selected five different batches of wheat flour. The flour was homogenized for one minute using a mixer and then sieved through a 90-mesh screen. A 2 ± 0.01 g sample from each batch was placed into the testing apparatus. The resulting five sets of sample curves are shown in
Figure 3. No significant differences were observed between the curves of the different sample groups. Furthermore, in food safety testing, the primary analytical objective is often not the precise determination of contaminant concentration but rather the reliable differentiation of contamination severity and associated risk levels [
31]. Under these conditions, strict quantitative regression may impose unrealistic assumptions regarding linear relationships, measurement precision, and concentration continuity. This study employs a semi-quantitative (ordinal) modeling strategy positioned between precise quantitative regression and nominal classification [
32]. This model does not predict exact borax concentrations but identifies ordered contamination levels, preserving severity rankings while reducing sensitivity to uncertainties in reference measurements.
Specifically, borax contamination levels were categorized into three ordinal groups based on regulatory relevance and practical risk differentiation: uncontaminated samples (0%), low-level contamination (0.1–0.9%), and high-level contamination (1–5%). These categories are encoded as ordinal labels 0, 1, and 2. This coding explicitly preserves the natural ordering among contamination levels while avoiding assumptions about linear distribution of concentration intervals. Based on this semi-quantitative framework, a classification model was developed to distinguish contamination levels. Beyond conventional classification accuracy, the random forest-based model was evaluated using an ordinal-aware performance metric that reflects both the magnitude and direction of misclassification. This approach not only assesses prediction correctness but also determines whether misclassifications occur between adjacent or distant contamination levels, providing more informative model reliability assessment for practical risk evaluation scenarios.
2.6. Baseline Classification Models
To establish performance benchmarks and evaluate the discriminative capability of microwave features under different learning paradigms, three representative classifiers were selected as reference models, as shown in
Figure 2C: support vector machine (SVM), backpropagation neural network (BP), and random forest (RF) [
31,
32,
33,
34]. These baselines cover complementary modeling philosophies, including kernel-based learning (SVM), shallow neural learning (BP), and ensemble learning (RF), and provide a fair benchmark prior to introducing feature selection and metaheuristic optimization. All baseline models were trained using the full 1800-dimensional feature vectors (after smoothing) without additional feature selection or hyperparameter optimization beyond standard settings.
The Support Vector Machine (SVM) classifier employs a Gaussian radial basis function (RBF) kernel and adopts a one-versus-one strategy for three-class classification in MATLAB (MathWorks, Natick, MA, USA, R2023b) [
35]. Prior to model training, a lightweight variance-based feature preselection is conducted using only the training data. Feature variances are calculated and ranked, and the top-ranked features are retained as inputs. Rather than serving as an independent feature selection algorithm, this procedure functions as a computationally efficient noise-reduction and dimensionality-control strategy, mitigating the adverse effects of redundant or low-information variables within the original 1800-dimensional feature space.
The Shallow Neural Learning model is implemented as a single-hidden-layer feedforward neural network trained using a standard backpropagation framework [
36]. A compact network architecture is adopted by limiting the number of hidden-layer neurons, and an early-stopping strategy based on validation performance is applied [
37]. During training, the available data are internally partitioned into training, validation, and monitoring subsets, and learning is terminated when validation performance fails to improve over consecutive iterations. This approach effectively constrains model complexity and reduces the risk of overfitting. To further enhance training stability, the number of iterations is restricted, and all preprocessing procedures, including normalization, are performed exclusively on the training data to prevent information leakage.
The baseline RF configuration constructs 25 trees with a minimum leaf size of 8 to prevent excessive tree depth. Random subsampling of predictor variables during each split effectively controls model complexity and inter-tree correlation. This configuration balances discriminative power, computational efficiency, and generalization stability.
2.7. Feature Selection and RF Optimization Frameworks
2.7.1. Proposed RF–WOA Joint Optimization Framework
This paper proposes a hybrid Random Forest-Whale Optimization Algorithm (RF-WOA) framework that collaboratively optimizes feature selection and Random Forest hyperparameters within a unified envelope optimization scheme. By processing feature selection and model optimization in series, the two processes are encoded as a single candidate solution [
38]. Specifically, each whale individual represents a solution vector
, where
is a continuous feature selection vector,
T denotes the number of trees in the RF model, and
L represents the minimum leaf size. The continuous feature vector
b is converted into a binary feature mask through a rule that partitions it based on distinguishing the magnitude of the
threshold values, where at least one feature is mandatorily retained to prevent degeneracy. The RF hyperparameters are searched within predefined ranges, with
and
.
The optimization objective is to maximize stratified five-fold cross-validated classification accuracy on the training subset while simultaneously discouraging overly large feature subsets. Accordingly, the fitness function is defined as
where
denotes the cross-validated accuracy,
k is the number of selected features,
is the total feature dimension, and
controls the trade-off between classification performance and feature sparsity. All fitness evaluations are conducted exclusively on the training data to prevent information leakage.
Compared with commonly used optimizers such as PSO and GA, the whale optimization algorithm (WOA) offers a balanced exploration–exploitation mechanism that helps mitigate premature convergence in high-dimensional feature spaces. This characteristic makes it well suited for jointly optimizing feature selection and RF hyperparameters in the 1800-dimensional microwave sensing dataset. The whale optimization algorithm is implemented with a population size of 18 and 80 iterations, following standard encircling, bubble-net attacking, and search-for-prey mechanisms with appropriate boundary-handling strategies. Through iterative updates, RF–WOA explores the joint feature–hyperparameter space and converges toward solutions that balance predictive accuracy and model compactness. After optimization, the RF classifier is retrained on the full training subset using the selected feature subset and optimized hyperparameters obtained from the best whale individual. The resulting model is then evaluated on the corresponding held-out test subset. Detailed parameter settings and search ranges are provided in
Table A1 to ensure transparency and reproducibility.
2.7.2. Proposed RF–VCPA Joint Optimization Framework
A comparative sequence feature selection and modeling framework based on Variable Combination Population Analysis (VCPA), termed RF-VCPA, employs a two-stage process: first performing independent feature selection, followed by training a Random Forest (RF) model on the refined feature subset. Variable Combination Population Analysis (VCPA) is a wrapper-based random feature selection method that iteratively identifies informative variables by evaluating populations of randomly generated feature subsets and progressively narrowing the candidate space [
39]. Variable combination population analysis (VCPA) is a stochastic, wrapper-based feature selection method that iteratively identifies informative variables by evaluating populations of randomly generated feature subsets and progressively shrinking the candidate space. Let
denote the original feature matrix, where
N is the number of samples and
is the total number of microwave features.
At iteration
t, VCPA maintains a candidate feature pool
,
. An initial retained ratio
was used, yielding
. For each iteration, a population of
candidate feature subsets is randomly generated by sampling combinations from
. Each candidate subset is evaluated using a random forest classifier trained on the training subset, and its performance is quantified by stratified cross-validated classification accuracy:
where
denotes the
i-th feature combination.
After evaluating the population, the top fraction
of candidate subsets with the highest accuracies is retained. Feature selection frequencies are then updated based on their occurrence within these top-performing subsets. It can be expressed by the following formula:
where
denotes the index set of the top-performing candidates and
is the indicator function. The candidate feature pool is subsequently reduced using a shrink factor
, such that
. The
features with the highest frequencies
are retained to form the updated pool
. This iterative process continues for a maximum of 35 iterations or until no further improvement in validation accuracy is observed.
Once VCPA converges, the final selected feature subset is used to train an RF classifier. To ensure a fair comparison with the RF–WOA framework, RF hyperparameters were tuned within constrained ranges rather than jointly optimized. Specifically, the number of trees was searched within , and the minimum leaf size within , from which the best-performing configuration (Trees , MinLeafSize ) was consistently obtained across runs. The parameter mtry, representing the number of predictors randomly sampled at each split, was set adaptively according to the dimensionality of the selected feature subset , following the default RF heuristic to maintain comparable model capacity across different feature set sizes.
RF–VCPA training and evaluation procedures were conducted exclusively on the training subset during feature selection to prevent information leakage, and final performance was assessed on the held-out test subset. Unlike RF–WOA, where feature selection and RF hyperparameters are encoded into a single optimization vector and jointly optimized under a unified objective function, RF–VCPA decouples these two processes. While this sequential strategy often yields highly compact feature subsets, it may overlook interactions between feature selection and classifier configuration, potentially limiting its ability to achieve globally optimal solutions under high-dimensional and class-imbalanced conditions.
2.8. Evaluation Metrics
Classification performance was primarily evaluated using overall accuracy (OA), together with macro-averaged precision (Macro-P), macro-averaged recall (Macro-R), and macro-averaged F1-score (Macro-F1), to provide a balanced assessment under the imbalanced three-class setting [
40]. Higher OA indicates stronger overall classification capability, while higher Macro-P, Macro-R, and Macro-F1 reflect improved average class-wise performance and a better balance between misclassification and omission errors across classes. In addition, per-class precision, recall, F1-score, and confusion matrices were analyzed to characterize class-specific classification behavior.
Under the semi-quantitative framework, model performance was evaluated using root mean square error (RMSE), mean absolute error (MAE), and the coefficient of determination (R2), which quantify the deviation between predicted and reference ordinal levels. Lower RMSE and MAE values indicate smaller ordinal prediction errors, reflecting higher consistency with the reference contamination grades, while higher R2 values indicate stronger agreement in the overall ordinal trend. These semi-quantitative regression results were jointly interpreted with classification performance to provide a comprehensive evaluation of model effectiveness for food safety screening and contamination risk-level assessment.
All classification metrics were summarized as mean ± standard deviation over 20 repeated stratified random splits to evaluate both predictive accuracy and model stability. To statistically compare RF–WOA with baseline RF and RF–VCPA models, paired t-tests and Wilcoxon signed-rank tests were conducted on paired results obtained from repeated runs, with statistical significance assessed at a 95% confidence level ( = 0.05).