Matlab Basics
🌐 Web Design | Development | YouTube Content Creation
📞 Contact: 9994905524
🚀 Learn Cloud Computing: www.aicloudcomputing.blogspot.com
Matlab code to generate Sin Wave:
clc;
clear;
close all;
% Define x from 0 to 2*pi with step size 0.01
x = 0:0.01:10*pi;
% Compute y = sin(x)
y = sin(x);
% Plot the sine wave
plot(x, y, 'r-', 'LineWidth', 2);
grid on;
% Add labels and title
xlabel('x (radians)');
ylabel('sin(x)');
title('Sine Waveform without linspace');
Matlab code to generate Sin Wave and Cosine Wave:
clc;
clear;
close all;
% Define step size
step = 0.01;
% Define x for sine wave from 0 to 2*pi
x1 = 0:step:2*pi;
y1 = sin(x1);
% Define x for cosine wave starting right after sine ends
% So it continues from 2*pi to 4*pi
x2 = (2*pi + step):step:(4*pi);
y2 = cos(x2);
% Combine the x and y vectors
x = [x1, x2];
y = [y1, y2];
% Plot the combined wave
plot(x, y, 'r-', 'LineWidth', 2);
grid on;
xlabel('x (radians)');
ylabel('Amplitude');
title('Sine Wave followed by Cosine Wave');
Matlab code to shift the phase of sin wave and cosine wave:
clc;
clear all;
close all;
step = 0.01;
phase_shift = pi/4; % Phase shift in radians
phase=pi/2;
x1 = 0:step:2*pi;
y1 = sin(x1); % Apply phase shift
x2 = (step+2*pi):step:4*pi;
y2 = cos(x2 + phase); % Apply phase shift
x = [x1, x2];
y = [y1, y2];
plot(x, y, 'r-', 'LineWidth', 2);
grid on;
title('Sine Wave with Phase Shift');
xlabel('x');
ylabel('sin(x + \phi)');
Matlab code for updating the number of waves in a second
clc;
clear all;
close all;
step = 0.01;
n=2;
phase_shift = pi/4; % Phase shift in radians
phase=pi/2;
x1 = 0:step:2*n*pi;
y1 = sin(x1); % Apply phase shift
x2 = (step+2*n*pi):step:4*n*pi;
y2 = sin(x2 + phase); % Apply phase shift
x = [x1, x2];
y = [y1, y2];
plot(x, y, 'r-', 'LineWidth', 2);
grid on;
title('Sine Wave with Phase Shift');
xlabel('x');
ylabel('sin(x + \phi)');
Matlab code for BPSK Modulated Signal:
fs = 1000; % Sampling frequency
t = 0:1/fs:5-(1/fs); % Time vector from 0 to 5 seconds
carfreq = 2; % Carrier frequency (5 Hz)
car = sin(2*pi*carfreq*t); % Carrier sine wave
% Define the bit pattern (BPSK)
bp = [1 -1 1 1 -1]; % Binary pattern
% Repeat the bit pattern to match the length of the time vector
bpsk_signal = []; % Initialize the BPSK signal
% Length of each bit duration
bit_duration = length(t) / length(bp);
% Generate BPSK signal by modulating the carrier with each bit
for i = 1:length(bp)
% Generate a section of the carrier signal modulated by the current bit
bpsk_signal = [bpsk_signal, bp(i) * car((i-1)*bit_duration + 1 : i*bit_duration)];
end
% Plot the resulting BPSK signal
figure;
plot(t, bpsk_signal, 'r-', 'LineWidth', 2);
grid on;
title('BPSK Signal');
xlabel('Time (s)');
ylabel('Amplitude');
MATLAB SAMPLE 1
clc;
clear;
close all;
x=[0 1 0 1 0];
y=1:5;
figure;
subplot(4,1,1);
stairs(y,x);
xlim([1 5]);
ylim([-1 2]);
grid on;
subplot(4,1,2);
stairs(y,x);
xlim([1 5]);
ylim([-1 2]);
grid on;
MATLAB SAMPLE 2
matrix = zeros(1,5);
z=zeros(1,5);
for i=1:5
n=5;
matrix(i)=z(i)+i;
end
disp(matrix);
MATLAB SAMPLE 3
matrix=[1 0 0 1 0];
z=zeros(1,5);
for i =1:5;
if(matrix(i)==0)
z(i)=-1;
else
z(i)=1;
end
end
disp(z);
brsa=xor(matrix,1);
disp(brsa);
ber analysis
snr = 0:0.1:20;
ebno=10.^(snr/10);
bere=0.5*erfc(sqrt(ebno));
plot(snr,bere);
axis([0 20 1e-5 1]);
set(gca, 'YScale', 'log');
Its very useful for me... ToolAra
ReplyDeleteCheckout the PPF calculator to know how much you can save with consistent investments. Ashiana Housing’s Home Loan PPF Calculator Thank you
ReplyDelete