-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVsquare.m
67 lines (48 loc) · 1.44 KB
/
Vsquare.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
% Program Vsquare.m
%%% Generation and Display of Periodic Square wave for
%%% Continuous Time Signals
%%% Developed by Dr. M. Venu Gopala Rao,
%%% Email: [email protected], [email protected],
% Program 1
% function y = Vsquare(x)
% % we set a default value of 1
% y = ones(1, length(x));
%
% % we find elements in the second half of the period by
% % dividing every cycle in two. The first half is odd,
% % the second half is even
% t = mod(ceil(x/pi), 2) == 0;
%
% % we set a -1 only for elements in the second half of every period
% % y(find(t)) = -1;
% y(t) = -1;
% % Program 2
% function y = Vsquare(x)
% % We'll inspect every value of the vector
% for i = 1 : length(x)
% % Total length of high and low values are pi
% t = ceil(x(i)/pi);
% if mod(t, 2)==0
% % if x(i) is in the last half, we set a -1
% y(i) = -1;
% else
% % if x(i) is in the first half, we set a +1
% y(i) = 1;
% end
% end
% % % Program 3
function y = Vsquare(x)
y = sin(x);
% we find positive elements and represent them as 1
y(find(y >= 0)) = 1;
% we find negative elements and represent them as -1
y(find(y < 0)) = -1;
%%% Demo:
%
% clear all;close all;clc;
% t = -8 : .01 : 8;
% x = Vsquare(t);
% plot(t,x,'r','LineWidth',1.5)
% axis([-8 8 -1.2 1.2])
% xlabel('time ----->');ylabel('Amplitude ----->');
% title('Square Wave'); grid on