Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab/Octave Question: Write a program to generate a column of numbers in Matla

ID: 3801792 • Letter: M

Question

Matlab/Octave Question:

Write a program to generate a column of numbers in Matlab or Octave. This column should be the sum of three or four sinusoidal functions (sine or cosine) at different frequencies as well as some randomly-generated “noise.” Something like:

f (x) = 12.6 sin (10x) 5.8 cos (4x) + randn(size(x)) (1)

would do nicely. Choose amplitudes for the sine waves that are much bigger than the noise (> factor of four) and choose frequencies that are integers less than 20. The point here is just that the amplitudes and frequencies are unknown to another student, not that they’re mean or tricky.

Write this column of data to a file.

Please do this as simply as possible, and include comments on every line explaining what is being done- even if it seems elementary.

Explanation / Answer

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
format long g;
format compact;
fontSize = 25;
fid = fopen('output.txt','w');
% Make a noisy sine wave signal
x = (0:0.001:1)';
%you can create a sample signal y consisting of two sinusoids, one at 50 Hz and one at 120 Hz with twice the amplitude.
y = sin(2*pi*50*x) + 2*sin(2*pi*120*x);
noiseAmplitude = 0.8;
y = y + noiseAmplitude * rand(size(y));
fwrite(fid,y);
fclose(fid);
subplot(2,1,1);
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
title('Noisy Signal', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')