← MATLAB EnglishChapter 12 of 13

Toolboxes

## Learning Objectives - Understand MATLAB toolbox ecosystem - Use common specialized functions - Identify when to use specific toolboxes ## Signal Processing Toolbox ### Signal Creation ```matlab % Basic signals t = 0:0.01:1; sine = sin(2*pi*5*t); % 5 Hz sine wave cosine = cos(2*pi*5*t); % 5 Hz cosine wave % Square wave sq = square(2*pi*5*t); % Sawtooth wave saw = sawtooth(2*pi*5*t); % Pulse train pulse = pulstran(t, 0:0.2:1, @rectpuls, 0.05); ``` ### Filtering ```matlab % Design filter fs = 1000; % Sampling frequency fc = 100; % Cutoff frequency [b, a] = butter(6, fc/(fs/2)); % 6th order Butterworth % Apply filter filtered = filter(b, a, signal); % Filter design methods [b, a] = cheby1(4, 3, fc/(fs/2)); % Chebyshev Type I [b, a] = ellip(4, 3, 40, fc/(fs/2)); % Elliptic ``` ### FFT and Spectral Analysis ```matlab % Fast Fourier Transform signal = sin(2*pi*50*t) + 0.5*sin(2*pi*120*t); N = length(signal); Y = fft(signal); P = abs(Y/N); % Frequency axis f = (0:N-1)*(fs/N); % Plot single-sided spectrum plot(f(1:N/2), P(1:N/2)) ``` ## Image Processing Toolbox ### Image Reading and Display ```matlab % Read image img = imread('image.png'); % Display imshow(img) % Image info info = imfinfo('image.png'); ``` ### Image Types ```matlab % RGB to grayscale gray = rgb2gray(img); % Grayscale to binary bw = imbinarize(gray); % Color spaces hsv = rgb2hsv(img); lab = rgb2lab(img); ``` ### Image Operations ```matlab % Resize resized = imresize(img, 0.5); % Rotate rotated = imrotate(img, 45); % Crop cropped = imcrop(img, [x, y, width, height]); % Histogram equalization eq = histeq(gray); ``` ### Morphological Operations ```matlab % Binary operations SE = strel('disk', 5); dilated = imdilate(bw, SE); eroded = imerode(bw, SE); opened = imopen(bw, SE); closed = imclose(bw, SE); % Edge detection edges = edge(gray, 'Canny'); ``` ## Optimization Toolbox ### Basic Optimization ```matlab % Find minimum of single-variable function f = @(x) x^2 - 3*x + 1; [xMin, fVal] = fminbnd(f, -10, 10); % Find minimum of multi-variable function f = @(x) (x(1)-2)^2 + (x(2)+1)^2; [xMin, fVal] = fminsearch(f, [0, 0]); ``` ### Constrained Optimization ```matlab % With constraints f = @(x) x(1)^2 + x(2)^2; A = [1, 2; 3, 2]; b = [6; 5]; x0 = [1; 1]; [x, fval] = fmincon(f, x0, A, b); % Bounds lb = [0; 0]; ub = [5; 5]; [x, fval] = fmincon(f, x0, A, b, [], [], lb, ub); ``` ### Linear and Quadratic Programming ```matlab % Linear programming f = [-1, -2]; A = [1, 2; 2, 1; -1, -2]; b = [6; 5; -3]; x = linprog(f, A, b); % Quadratic programming H = [2, 0; 0, 2]; f = [-2; -4]; x = quadprog(H, f); ``` ## Statistics and Machine Learning Toolbox ### Machine Learning Basics ```matlab % Train simple classifier X = randn(100, 2); y = X(:, 1) + X(:, 2) > 0; % Fit model mdl = fitcsvm(X, y); % Predict [label, score] = predict(mdl, X); ``` ### Clustering ```matlab % K-means clustering data = randn(100, 2) + [randn(50, 2); randn(50, 2) + 5]; [idx, C] = kmeans(data, 2); % Hierarchical clustering Z = linkage(data, 'ward'); dendrogram(Z); ``` ### Classification Learner ```matlab % Export model from Classification Learner app % load trained model load trainedModel.mat predictions = predict(trainedModel, newData); ``` ## Curve Fitting Toolbox ### Basic Fitting ```matlab x = 0:0.1:10; y = 2*exp(-0.3*x) + 0.5*sin(x) + randn(1, 101)*0.1; % Fit exponential f = fit(x', y', 'exp1'); % Fit polynomial f = fit(x', y', 'poly2'); % Custom equation f = fit(x', y', 'a*exp(-b*x)+c'); ``` ## Control System Toolbox ### Transfer Functions ```matlab % Create transfer function num = [1, 2]; den = [1, 3, 2]; sys = tf(num, den); % Bode plot bode(sys) % Step response step(sys) % PID controller Kp = 1; Ki = 0.5; Kd = 0.1; pid = pid(Kp, Ki, Kd); ``` ## Symbolic Math Toolbox ### Symbolic Variables ```matlab % Create symbolic variables syms x y z % Expression f = x^2 + 2*x + 1; g = (x + 1)^2; % Simplify simplify(f - g) % 0 % Expand and factor expand(f) % x^2 + 2*x + 1 factor(x^2 - 1) % (x-1)(x+1) ``` ### Calculus ```matlab syms x % Differentiation f = x^3 + 2*x^2; df = diff(f, x); % 3*x^2 + 4*x % Integration int(f, x) % x^4/4 + 2*x^3/3 % Limits limit(sin(x)/x, x, 0) % 1 ``` ### Solving Equations ```matlab syms x y % Solve equation solve(x^2 - 4 == 0, x) % [-2, 2] % Solve system eqns = [x + y == 3, x - y == 1]; S = solve(eqns, [x, y]); S.x % 2 S.y % 1 ``` ## Parallel Computing Toolbox ### Parallel Loops ```matlab % Parallel for loop parfor i = 1:1000 result(i) = heavyComputation(i); end % Check parallel pool parpool ``` ### GPU Computing ```matlab % Move array to GPU gdata = gpuArray(data); % GPU array operations gresult = sqrt(gdata); % Gather result result = gather(gresult); ``` ## Data Science Toolbox ### Data Manipulation ```matlab % Detect missing values ismissing(data) % Remove missing clean = rmmissing(data); % Fill missing filled = fillmissing(data, 'linear'); % Moving average movmean(data, 5) ``` ## Mapping Toolbox ```matlab % Create geographic data lat = [40.7, 40.8, 40.9]; lon = [-74.0, -73.9, -73.8]; geoplot(lat, lon, 'o-') % Add basemap geobasemap streets ``` ## Checklist for Common Tasks | Task | Toolbox | |------|---------| | Signal filtering | Signal Processing | | Image processing | Image Processing | | Optimization | Optimization | | Machine learning | Statistics and Machine Learning | | Symbolic math | Symbolic Math | | Control systems | Control System | | Parallel computing | Parallel Computing | | Curve fitting | Curve Fitting | ## Summary - MATLAB toolboxes extend base functionality - Signal Processing: filters, FFT, spectral analysis - Image Processing: filters, morphology, transforms - Optimization: linear/nonlinear optimization - Symbolic Math: analytical calculations - Parallel Computing: speed up with multiple cores/GPUs - Use `ver` to check installed toolboxes

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →