0% found this document useful (0 votes)
29 views2 pages

Basic Codes For Matlab: 'Sine Wave' 'Time' 'Amplitude'

This document contains MATLAB code examples and explanations for: 1) Plotting a sine wave with given amplitude and frequency. 2) Generating and playing a sine wave sound. 3) Creating ones and zeros matrices and an identity matrix. 4) Generating random numbers and finding the size and length of vectors. 5) Assigning complex values and extracting real and imaginary parts. 6) Generating a random matrix and replacing values based on a threshold. 7) How to create user-defined functions and generate executable files from MATLAB code.

Uploaded by

ShajiaSiddiqui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Basic Codes For Matlab: 'Sine Wave' 'Time' 'Amplitude'

This document contains MATLAB code examples and explanations for: 1) Plotting a sine wave with given amplitude and frequency. 2) Generating and playing a sine wave sound. 3) Creating ones and zeros matrices and an identity matrix. 4) Generating random numbers and finding the size and length of vectors. 5) Assigning complex values and extracting real and imaginary parts. 6) Generating a random matrix and replacing values based on a threshold. 7) How to create user-defined functions and generate executable files from MATLAB code.

Uploaded by

ShajiaSiddiqui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

BASIC CODES FOR MATLAB

%To plot 3 cycles of sine wave of 20kHz and amplitude of 10


t= 0:0.0000001:0.00015;
y=10*sin(2*pi*20000*t);
plot(t,y);
title('Sine Wave')
xlabel('Time')
ylabel('Amplitude')

%To generate sound of sine wave having amplitude of 3 and frequency of


%20Khz
t= 0:0.0001:10;
y=3*sin(2*pi*20000*t);
wavplay(y,40000)

% ones of 4 x4
y = ones(4)

% ones % 1 row : 10 column


i = ones(1,10)

% zeros
% 1 row : 10 column
i = zeros(1,10)

% eye
% diagonal elements 1
i = eye(5)
% inverted eye % diagonal elements 0
i = ~eye(5)
% random numbers generation 5 x 5
i = rand(5)
% Size Command

y = size(t)

% Length Command

y = length(t)

% Assigning complex value

y = 7 + 6j

% Real and complex parts


g = real(y)
h = imag(y)

% Conjugate
k = conj(y)
%Generate the random matrix of 1x10 and check if any number is greater or
%equal to 0.5 make it 1

a= rand(1,10);
for m=1:10;
if a(1,m)>=0.5;
a(1,m)=1;
end
end

%How to make USER Defined function in MATLAB

Important points

1. Function is something that is called not run


2. File name and function name should same
3. Current directory

function dsp()
clc;
figure (10);

%How to make EXE. File of MATLAB

pcode dsp.m
mcc -m dsp.m

You might also like