## Density Estimation Using Wavelets

This section takes you through the features of 1-D wavelet density estimation using one of the Wavelet Toolbox™ specialized tools.

The toolbox provides Wavelet Analyzer app to estimate the density of a sample and complement well known tools like the histogram (available from the MATLAB® core) or kernel based estimates.

For the examples in this section, switch the extension mode to symmetric padding, using the command

```dwtmode('sym') ```

### 1-D Estimation Using the Wavelet Analyzer App

1. Start the Density Estimation 1-D Tool.

From the MATLAB prompt, type `waveletAnalyzer`.

The Wavelet Analyzer appears.

Click the menu item. The discrete wavelet analysis tool for 1-D density estimation appears.

At the MATLAB command line, type

`load ex1cusp1`
In the Density Estimation 1-D tool, choose File > Import from Workspace.

When the Import from Workspace dialog box appears, select `ex1cusp1`. Click OK to import the noisy cusp data.

The sample, a 64-bin histogram, and the processed data obtained after a binning are displayed. In this example, we'll accept the default value for the number of bins (250). The binned data, suitably normalized, will be processed by wavelet decomposition.

3. Perform a Wavelet Decomposition of the binned data.

Select the `sym6` wavelet from the Wavelet menu and select 4 from the Level menu, and click the Decompose button. After a pause for computation, the tool displays the detail coefficients of the decomposition of the binned data.

4. Perform a density estimation.

Accept the defaults of global soft thresholding. The sliders located on the right of the window control the level dependent thresholds, indicated by dashed blue lines running horizontally through the graphs on the left of the window.

Continue by clicking the Estimate button.

You can see that the estimation process delivers a very irregular resulting density. The density estimate (in purple) is the normalized sum of the signals located below it: the approximation `a4` and the reconstructed details after coefficient thresholding.

5. Perform thresholding.

You can experiment with the various predefined thresholding strategies by selecting the appropriate options from the menu located on the right of the window or directly by dragging the dashed blue lines with the left mouse button. Let's try another estimation method.

From the menu Select thresholding method, select the item By level threshold 2. Next, click the Estimate button.

The estimated density is more satisfactory. It correctly identifies the smooth part of the density and the cusp at 0.7.

### Importing and Exporting Information from the Wavelet Analyzer App

The tool lets you save the estimated density to disk. The toolbox creates a MAT-file in the current folder with a name you choose.

To save the estimated density, use the menu option File > Save Density. A dialog box appears that lets you specify a folder and filename for storing the density. Type the name `dex1cusp`. After saving the density data to the file `dex1cusp.mat`, load the variables into your workspace:

```load dex1cusp whos ```
NameSizeBytesClass
`ex1cusp1``1x1000``8000``double array`
`thrParams``1x4``544``cell array`
`wname``1x4``8``char array`
`xdata``1x250``2000``double array`
`ydata``1x250``2000``double array`

The original noisy cusp data `ex1cusp1` has 1000 samples. The variables `thrParams`, `wname`, `xdata`, and `ydata` are stored in `dex1cusp.mat`. The estimated density is given by `xdata` and `ydata`. The length of these vectors is equal to the number of bins you choose in step 4. In addition, the parameters of the estimation process are given by the wavelet name in `wname`.

```wname wname = sym6 ```

and the level dependent thresholds contained in `thrParams`, which is a cell array of length 4 (the level of the decomposition). For i from 1 to 4, `thrParams{i}` contains the lower and upper bounds of the interval of thresholding and the threshold value (since interval dependent thresholds are allowed). For more information, see 1-D Adaptive Thresholding of Wavelet Coefficients. For example, for level 1,

```thrParams{1} ans = 0.0560 0.9870 2.1179 ```

Note

When you load data from a file using the menu option File > Load Data for Density Estimate, the first 1-D variable encountered in the file is considered the signal. Variables are inspected in alphabetical order.

At the end of this section, turn the extension mode back to zero padding using

```dwtmode('zpd') ```

Get trial now