Hello,
I'm not sure how to go about write a function for a sinusoid that oscillates between 0 and 0.1 with a period of 1 second. The sinusoid should be sampled at 2ms and plotted for the first 10 seconds. Thank you in advance.
sinusoid
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;% Setup: create the signal.
x = linspace(-100, 100, 800); % 800 points from -100 to +100
% Define the period of the sine wave, and it's amplitude
period = 40;amplitude = 15;phase = 5;% Create the signal
y = amplitude * sin(2 * pi * x / period + phase);subplot(2, 1, 1);plot(x, y, 'b-', 'LineWidth', 2);title('Original Signal', 'FontSize', fontSize);ylabel('y', 'FontSize', fontSize);xlabel('x', 'FontSize', fontSize);grid on;ax = gca;ax.XAxisLocation = 'origin';ax.YAxisLocation = 'origin';xticks(-100:5:100);% Engine: compute a localized RMS
% Square the signal.
ySquared = y .^ 2;% Get a moving average within a window
windowWidth = 121;meanSquareY = movmean(ySquared, windowWidth);% Take the square root of the mean square signal.
localRMSy = sqrt(meanSquareY);% Plot those
subplot(2, 1, 2);plot(x, ySquared, 'b-', 'LineWidth', 2);hold on;plot(x, localRMSy, 'r-', 'LineWidth', 2);caption = sprintf('Local RMS in a moving window of width %d', windowWidth);title(caption, 'FontSize', fontSize);ylabel('Signal', 'FontSize', fontSize);xlabel('x', 'FontSize', fontSize);grid on;ax = gca;ax.XAxisLocation = 'origin';ax.YAxisLocation = 'origin';xticks(-100:5:100);legend('y squared', 'local RMS');% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;%===============================================================================
% Read in gray scale demo image.
folder = pwd; baseFileName = 'seg.jpg';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Display the image.
subplot(2, 2, 1);imshow(rgbImage, []);title('Original RGB Color Image', 'FontSize', fontSize, 'Interpreter', 'None');axis('on', 'image');hp = impixelinfo();% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)if numberOfColorChannels > 1 % It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else grayImage = rgbImage; % It's already gray scale.
end% Now it's gray scale with range of 0 to 255.
% Display the histogram of the image.
subplot(2, 2, 2);[counts, binLocations] = imhist(grayImage);grid on;title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')drawnow;% Binarize the image
binaryImage = grayImage > 128;% Crop off the white frame
allWhiteRows = all(binaryImage, 2);binaryImage = binaryImage(~allWhiteRows, :);allWhiteColumns = all(binaryImage, 1);binaryImage = binaryImage(:, ~allWhiteColumns);% Display the image.subplot(2, 2, 2);imshow(binaryImage, []);title('Cropped Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');axis('on', 'image');hp = impixelinfo();% Get the horizontal count profile
horizontalProfile = sum(binaryImage, 1);% Display the plot.
subplot(2, 3, 4);plot(horizontalProfile, 'b-', 'LineWidth', 2);grid on;title('Horizontal Profile', 'FontSize', fontSize, 'Interpreter', 'None');% Find the max in the left half.
midColumn = round(columns/2)[~, leftMaxIndex] = max(horizontalProfile(1:midColumn))% Find the max in the right half.
[~, rightMaxIndex] = max(horizontalProfile(midColumn+1:end));rightMaxIndex = rightMaxIndex + midColumn% Draw lines at peaks.
hold on;line([leftMaxIndex, leftMaxIndex], ylim, 'Color', 'r', 'LineWidth', 2);line([rightMaxIndex, rightMaxIndex], ylim, 'Color', 'r', 'LineWidth', 2);% Now find the minimum in between the max:
% Find the max in the right half.[~, minIndex] = min(horizontalProfile(leftMaxIndex+1:rightMaxIndex-1));minIndex = minIndex + leftMaxIndexline([minIndex, minIndex], ylim, 'Color', 'r', 'LineWidth', 2);% Now crop the image into right and left halves:
leftImage = binaryImage(:, 1:minIndex);rightImage = binaryImage(:, minIndex+1:end);% Display the images.
subplot(2, 3, 5);imshow(leftImage);title('Left Half Image', 'FontSize', fontSize, 'Interpreter', 'None');axis('on', 'image');subplot(2, 3, 6);imshow(rightImage);title('Right Half Image', 'FontSize', fontSize, 'Interpreter', 'None');axis('on', 'image');
Best Answer