## 2 Materials and Methods
### The Discrete Wavelet Transform Process  
The scaling function can be expressed in the wavelet from minus infinity to a certain scale. If the wavelet spectrum is added to the scaling function spectrum, the new scaling performance is obtained with a double-width spectrum. We can express this relation in the so-called multiresolution formulation or two-scale relation (Figure 11) [19; 20]:  
\[\varphi\big{(}2^{i}t\big{)}=\sum_{k}h_{j+1}(k)\varphi\big{(}2^{j+1}t-k\big{)}. \tag{1}\]  
Figure 10: Wavelet transform. The two-scale relation states that the scaling function at a certain scale can be expressed in terms of translated scaling functions with more detail. The first scaling function replaced a set of wavelets, and therefore, it can be expressed in terms of translated scaling functions at the next scale. More specifically, it can be written for the wavelet at level j (Figure 11):  
\[\psi\big{(}2^{j}t\big{)}=\sum_{k}g_{j+1}(k)\varphi\big{(}2^{j+1}t-k\big{)} \tag{2}\]  
which is the two-scale relation between the scaling function and the wavelet. Signal \(f\)(t) can also be expressed in terms of dilated and translated scaling functions at a scale j:  
\[f(t)=\sum_{k}\lambda_{j}(k)\varphi\big{(}2^{j}t-k\big{)} \tag{3}\]  
By considering a scale to \(j-1\) in this equation, the wavelets are added in order to keep the same level of detail. The signal \(f\)(t) is expressed as follows:  
\[f(t)=\sum_{k}\lambda_{j-1}(k)\varphi\Big{(}2^{j-1}t-k\Big{)}+\sum_{k}\gamma_{ j}(k)\psi\Big{(}2^{j-1}t-k\Big{)} \tag{4}\]  
If the scaling function \(\varphi_{j,k}(t)\) and the wavelets \(\psi_{j,k}(t)\) are orthonormal or a tight frame, then the coefficients \(\lambda_{j-1}(k)\) and \(\gamma_{j-1}(k)\) are found by taking the inner products;  
\[\lambda_{j-1}(k)=f(t),\varphi_{j,k}(t) \tag{5}\]  
\[\gamma_{j-1}(k)=f(t),\psi_{j,k}(t) \tag{6}\]  
If \(\varphi_{j,k}(t)\) is replaced with \(\psi_{j,k}(t)\) in the inner products by suitably scaled and translated versions of (19) and (20), the following equations are resulted [21]:  
\[\lambda_{j-1}(k)=\sum_{m}h(m-2k)\lambda_{j}(m) \tag{7}\]  
\[\gamma_{j-1}(k)=\sum_{m}g(m-2k)\lambda_{j}(m) \tag{8}\]  
These two equations state that by calculating the sum weighted sum of the scaling function coefficient from the previous scale, we can find wavelet and scaling function coefficients at the specified scale. Figures 12 and 13 indicate a discrete wavelet transform for an arbitrary signal in three stages [17; 21; 22; 23]. For this work, the peak values of signal range (\(v\)), in the time domain for each signal at 15.8 milliseconds were sampled. Then, based on the specified number of samples, these values at first were stored in a vector, and later approximation coefficients (CA) and detailed coefficients (CD) obtained from signal wavelet transform were calculated using MATLAB software. The wavelet transform was calculated through wavedec MATLAB code. The calculated coefficients were considered as signal features [17; 18]. Figure 11: Decomposition at level 3: f(t) = d1 + d2 + d3 + a3. Sorting and Grading by an Artificial Neural Network  
In order to sort and grade hazelnuts, multilayer perceptron (MLP) neural networks with a backpropagation learning method were used. The neural network program was written in MATLAB using a wavelet toolbox. The approximation coefficients (ACs) and detailed coefficients (DCs) obtained from the wavelet transform of each hazelnut signal were selected as network inputs to classify the hazelnuts into three groups [16; 17]. In this network, iteration numbers and MSE of the learning data were considered powerful validation criteria because they impact the selection of the best training in optimal generalization point, the reduction of experimental errors, and the prevention of overtraining data. The Levenberg-Marquardt algorithm was used to train the network. In order to find the optimal value of the structure neural network, the k-fold cross validation was applied to perform the classification.