### Install Sphinx Documentation Tool Source: https://github.com/matousc89/padasip/blob/master/docs/README.txt Installs the Sphinx documentation generator via the apt package manager. ```bash sudo apt-get install python3-sphinx ``` -------------------------------- ### Build Documentation with Sphinx Source: https://github.com/matousc89/padasip/blob/master/docs/README.txt Commands to generate HTML documentation from the source directory. ```bash sphinx-build -b html source/ build/ ``` ```bash python -m sphinx -b html source/ build/ ``` -------------------------------- ### Implement Affine Projection Filter Source: https://context7.com/matousc89/padasip/llms.txt Use the FilterAP class for faster convergence with correlated inputs. The projection order and regularization parameter (ifc) are key configuration settings. ```python import numpy as np import padasip as pa N = 500 x = np.random.normal(0, 1, (N, 4)) v = np.random.normal(0, 0.1, N) d = 2*x[:,0] + 0.1*x[:,1] - 4*x[:,2] + 0.5*x[:,3] + v # AP filter with projection order 5 f = pa.filters.FilterAP(n=4, order=5, mu=0.5, ifc=0.001, w="random") y, e, w = f.run(d, x) # order: number of input vectors used (projection order) # ifc: regularization for matrix inversion stability print(f"Final weights: {w[-1]}") ``` -------------------------------- ### Perform Pretrained Filter Run Source: https://context7.com/matousc89/padasip/llms.txt Train a filter on a subset of data for a specified number of epochs before testing on the remaining portion. ```python import numpy as np import padasip as pa N = 1000 x = np.random.normal(0, 1, (N, 4)) v = np.random.normal(0, 0.1, N) d = 2*x[:,0] + 0.1*x[:,1] - 4*x[:,2] + 0.5*x[:,3] + v f = pa.filters.FilterNLMS(n=4, mu=0.5, w="zeros") # Train on first 50% of data for 3 epochs, test on remaining 50% y, e, w = f.pretrained_run(d, x, ntrain=0.5, epochs=3) # y, e correspond to test portion only (last 500 samples) print(f"Test set length: {len(y)}") # 500 print(f"Test MSE: {np.mean(e**2):.6f}") ``` -------------------------------- ### Instantiate Filters with AdaptiveFilter Factory Source: https://context7.com/matousc89/padasip/llms.txt Use the AdaptiveFilter factory function to create and run adaptive filters by specifying the model type. This approach is useful for switching between different algorithms using a consistent interface. ```python import numpy as np import padasip as pa # Create sample data for channel identification N = 500 x = np.random.normal(0, 1, (N, 4)) # input matrix (N samples, 4 features) v = np.random.normal(0, 0.1, N) # additive noise d = 2*x[:,0] + 0.1*x[:,1] - 4*x[:,2] + 0.5*x[:,3] + v # target signal # Create NLMS filter using factory function f = pa.filters.AdaptiveFilter(model="NLMS", n=4, mu=0.1, w="random") # Run batch filtering y, e, w = f.run(d, x) # Results: # y - filter output (500,) # e - prediction error (500,) # w - weight history (500, 4) print(f"Final weights: {w[-1]}") # Should approximate [2, 0.1, -4, 0.5] print(f"Final MSE: {np.mean(e[-100:]**2):.6f}") ``` -------------------------------- ### Perform System Identification Source: https://context7.com/matousc89/padasip/llms.txt Compare performance of different adaptive filters (LMS, NLMS, RLS) for identifying an unknown system. ```python import numpy as np import padasip as pa # Unknown system to identify: y = 2*x1 + 0.1*x2 - 4*x3 + 0.5*x4 + noise np.random.seed(42) N = 1000 true_weights = np.array([2.0, 0.1, -4.0, 0.5]) n = len(true_weights) x = np.random.normal(0, 1, (N, n)) v = np.random.normal(0, 0.1, N) d = x @ true_weights + v # Compare different filters filters = { "LMS": pa.filters.FilterLMS(n=n, mu=0.01, w="zeros"), "NLMS": pa.filters.FilterNLMS(n=n, mu=0.5, w="zeros"), "RLS": pa.filters.FilterRLS(n=n, mu=0.99, w="zeros"), } results = {} for name, f in filters.items(): y, e, w = f.run(d, x) results[name] = { "final_weights": w[-1], "weight_error": np.linalg.norm(w[-1] - true_weights), "mse_first_100": pa.misc.MSE(e[:100]), "mse_last_100": pa.misc.MSE(e[-100:]), } print(f"\n{name}:") print(f" Final weights: {w[-1].round(3)}") print(f" Weight error: {results[name]['weight_error']:.4f}") print(f" MSE (first 100): {results[name]['mse_first_100']:.4f}") print(f" MSE (last 100): {results[name]['mse_last_100']:.4f}") ``` -------------------------------- ### Implement Least Mean Squares (LMS) Filter Source: https://context7.com/matousc89/padasip/llms.txt The FilterLMS class supports both batch processing via run() and real-time sample-by-sample processing using predict() and adapt(). Requires careful tuning of the learning rate mu. ```python import numpy as np import padasip as pa # Batch filtering example N = 500 x = np.random.normal(0, 1, (N, 4)) v = np.random.normal(0, 0.1, N) d = 2*x[:,0] + 0.1*x[:,1] - 4*x[:,2] + 0.5*x[:,3] + v # Create and run LMS filter f = pa.filters.FilterLMS(n=4, mu=0.01, w="random") y, e, w = f.run(d, x) # Real-time filtering example filt = pa.filters.FilterLMS(n=3, mu=0.1, w="zeros") for k in range(100): x_sample = np.random.random(3) d_sample = 2*x_sample[0] + 1*x_sample[1] - 1.5*x_sample[2] # Predict before seeing target y_pred = filt.predict(x_sample) # Update weights after observing target filt.adapt(d_sample, x_sample) print(f"Final weights: {filt.w}") # Approximates [2, 1, -1.5] ``` -------------------------------- ### Evaluate Filter Convergence Source: https://context7.com/matousc89/padasip/llms.txt Calculate and print the Mean Squared Error (MSE) at different stages of filter execution. ```python print(f"Final weights: {w[-1]}") print(f"MSE after 50 samples: {np.mean(e[40:50]**2):.6f}") print(f"MSE after 500 samples: {np.mean(e[-50:]**2):.6f}") ``` -------------------------------- ### Detect Novelties with ELBND Source: https://context7.com/matousc89/padasip/llms.txt Combine filter error and weight changes to detect novelties using max or sum aggregation functions. ```python import numpy as np import padasip as pa # Create data with anomaly n = 5 N = 2000 x = np.random.normal(0, 1, (N, n)) d = np.sum(x, axis=1) + np.random.normal(0, 0.1, N) d[1000] += 2.0 # anomaly # Run adaptive filter f = pa.filters.FilterNLMS(n, mu=1., w=np.ones(n)) y, e, w = f.run(d, x) # ELBND with max function (default) elbnd_max = pa.detection.ELBND(w, e, function="max") # ELBND with sum function elbnd_sum = pa.detection.ELBND(w, e, function="sum") # Detect anomaly anomaly_idx = np.argmax(elbnd_max) print(f"Detected anomaly at sample: {anomaly_idx}") print(f"ELBND(max) value: {elbnd_max[anomaly_idx]:.4f}") print(f"ELBND(sum) value: {elbnd_sum[anomaly_idx]:.4f}") ``` -------------------------------- ### Calculate Error Metrics Source: https://context7.com/matousc89/padasip/llms.txt Compute standard error metrics like MSE, MAE, and RMSE to evaluate the performance of adaptive filters. ```python import numpy as np import padasip as pa # Two methods: from two series or from error directly x1 = np.array([1, 2, 3, 4, 5]) x2 = np.array([1.1, 2.2, 2.9, 4.1, 5.0]) # From two series mse = pa.misc.MSE(x1, x2) mae = pa.misc.MAE(x1, x2) rmse = pa.misc.RMSE(x1, x2) print(f"MSE: {mse:.4f}, MAE: {mae:.4f}, RMSE: {rmse:.4f}") ``` -------------------------------- ### Apply Principal Component Analysis (PCA) Source: https://context7.com/matousc89/padasip/llms.txt Reduce input dimensionality by projecting data onto principal components, useful for preprocessing high-dimensional or correlated features. ```python import numpy as np import padasip as pa # Generate correlated data np.random.seed(42) x = np.random.randn(100, 5) x[:, 1] = x[:, 0] + 0.1*np.random.randn(100) # correlated features x[:, 2] = x[:, 0] - 0.1*np.random.randn(100) # Check eigenvalues to decide number of components eigenvalues = pa.preprocess.PCA_components(x) print(f"Eigenvalues: {eigenvalues") # First few eigenvalues much larger = good candidates for reduction # Reduce from 5 to 3 dimensions x_reduced = pa.preprocess.PCA(x, n=3) print(f"Original shape: {x.shape}") # (100, 5) print(f"Reduced shape: {x_reduced.shape}") # (100, 3) # Use reduced data with adaptive filter d = np.sum(x_reduced, axis=1) + 0.1*np.random.randn(100) f = pa.filters.FilterNLMS(n=3, mu=0.5) y, e, w = f.run(d, x_reduced) ``` -------------------------------- ### Construct Input Matrix from History Source: https://context7.com/matousc89/padasip/llms.txt Generate sliding window input matrices for time series prediction tasks, with optional bias term support. ```python import numpy as np import padasip as pa # Create input matrix from time series signal = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Create windows of size 3 x = pa.input_from_history(signal, n=3) print(x) # Output: # [[1 2 3] # [2 3 4] # [3 4 5] # [4 5 6] # [5 6 7] # [6 7 8] # [7 8 9] # [8 9 10]] # With bias term (adds column of ones) x_bias = pa.input_from_history(signal, n=3, bias=True) print(x_bias.shape) # (8, 4) - extra column for bias # Time series prediction example N = 200 t = np.linspace(0, 4*np.pi, N) signal = np.sin(t) + 0.1*np.random.randn(N) # Use last 5 samples to predict next sample n = 5 x = pa.input_from_history(signal[:-1], n) # inputs d = signal[n:] # targets (shifted by n) f = pa.filters.FilterNLMS(n=n, mu=0.5) y, e, w = f.run(d, x) print(f"Prediction MSE: {np.mean(e[-50:]**2):.6f}") ``` -------------------------------- ### Detect Anomalies with Learning Entropy Source: https://context7.com/matousc89/padasip/llms.txt Identify novelties by analyzing weight update behavior in adaptive filters using direct or multiscale approaches. ```python import numpy as np import padasip as pa # Create data with anomaly at sample 1000 n = 5 N = 2000 x = np.random.normal(0, 1, (N, n)) d = np.sum(x, axis=1) + np.random.normal(0, 0.1, N) # Insert perturbation (anomaly) d[1000] += 2.0 # Run adaptive filter to get weight history f = pa.filters.FilterNLMS(n, mu=1., w=np.ones(n)) y, e, w = f.run(d, x) # Direct approach (no alpha specified) le_direct = pa.detection.learning_entropy(w, m=30, order=1) # Multiscale approach (with alpha sensitivities) le_multiscale = pa.detection.learning_entropy( w, m=30, order=2, alpha=[8., 9., 10., 11., 12., 13.] ) # Find anomaly peak anomaly_idx = np.argmax(le_multiscale) print(f"Detected anomaly at sample: {anomaly_idx}") # Should be ~1000 print(f"LE value at anomaly: {le_multiscale[anomaly_idx]:.4f}") ``` -------------------------------- ### Implement Recursive Least Squares (RLS) Filter Source: https://context7.com/matousc89/padasip/llms.txt FilterRLS offers faster convergence than LMS-based methods by maintaining an inverse autocorrelation matrix. The mu parameter acts as a forgetting factor, typically set close to 1. ```python import numpy as np import padasip as pa N = 500 x = np.random.normal(0, 1, (N, 4)) v = np.random.normal(0, 0.1, N) d = 2*x[:,0] + 0.1*x[:,1] - 4*x[:,2] + 0.5*x[:,3] + v # RLS filter with forgetting factor mu (close to 1 for stability) f = pa.filters.FilterRLS(n=4, mu=0.99, eps=0.1, w="zeros") y, e, w = f.run(d, x) ``` -------------------------------- ### Standardize Data for Stability Source: https://context7.com/matousc89/padasip/llms.txt Normalize data using z-score standardization to improve adaptive filter convergence. Includes support for custom offsets and reversing the transformation. ```python import numpy as np import padasip as pa # Generate data with non-zero mean and varying scale x = np.random.normal(50, 10, 1000) print(f"Original - Mean: {x.mean():.2f}, Std: {x.std():.2f}") # Standardize to zero mean, unit variance xs = pa.standardize(x) print(f"Standardized - Mean: {xs.mean():.6f}, Std: {xs.std():.6f}") # Custom offset and scale xs_custom = pa.standardize(x, offset=50, scale=10) print(f"Custom standardization - Mean: {xs_custom.mean():.2f}") # Reverse standardization from padasip.preprocess import standardize_back x_restored = standardize_back(xs, offset=x.mean(), scale=x.std()) print(f"Restored matches original: {np.allclose(x, x_restored)}") ``` -------------------------------- ### Implement Normalized Least Mean Squares (NLMS) Filter Source: https://context7.com/matousc89/padasip/llms.txt FilterNLMS provides improved stability for signals with varying power levels. The eps parameter is used to prevent division by zero during normalization. ```python import numpy as np import padasip as pa # NLMS is more stable than LMS for varying input power N = 500 x = np.random.normal(0, 1, (N, 4)) v = np.random.normal(0, 0.1, N) d = 2*x[:,0] + 0.1*x[:,1] - 4*x[:,2] + 0.5*x[:,3] + v # Create NLMS filter with regularization term eps f = pa.filters.FilterNLMS(n=4, mu=0.5, eps=0.001, w="random") y, e, w = f.run(d, x) # mu can be larger (0 to 2) due to normalization # eps prevents division by zero for small inputs print(f"Final weights: {w[-1]}") print(f"MSE: {np.mean(e**2):.6f}") ``` -------------------------------- ### Calculate Error Metrics Source: https://context7.com/matousc89/padasip/llms.txt Compute Mean Squared Error (MSE) and Logarithmic Squared Error (logSE) from filter outputs or raw error signals. ```python # From error directly e = x1 - x2 mse_from_e = pa.misc.MSE(e) print(f"MSE from error: {mse_from_e:.4f}") # Logarithmic squared error (returns dB values) logse = pa.misc.logSE(x1, x2) print(f"logSE (dB): {logse}") # Practical usage with filter output N = 500 x = np.random.normal(0, 1, (N, 4)) d = 2*x[:,0] - 3*x[:,1] + np.random.normal(0, 0.1, N) f = pa.filters.FilterNLMS(n=4, mu=0.5) y, e, w = f.run(d, x) print(f"Overall MSE: {pa.misc.MSE(e):.6f}") print(f"Converged MSE (last 100): {pa.misc.MSE(e[-100:]):.6f}") ``` -------------------------------- ### Perform Direct Data Filtering Source: https://context7.com/matousc89/padasip/llms.txt Use filter_data for a one-liner filtering operation that automatically infers filter size from input dimensions. This is ideal for quick processing without manual filter object management. ```python import numpy as np import padasip as pa # Generate test data N = 1000 x = np.random.normal(0, 1, (N, 3)) d = 1.5*x[:,0] - 2.0*x[:,1] + 0.8*x[:,2] + np.random.normal(0, 0.05, N) # Filter data in one line - filter size is inferred from x.shape[1] y, e, w = pa.filters.filter_data(d, x, model="LMS", mu=0.01, w="zeros") # Evaluate performance mse = pa.misc.MSE(e[-200:]) print(f"Converged MSE: {mse:.6f}") print(f"Estimated coefficients: {w[-1]}") # Should approximate [1.5, -2.0, 0.8] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.