You are on page 1of 5

09. 1. 6 4:09 C:\Users\\Documents\MATLAB\rectify.

m 1 of 1
% ECE 351 matlab exercise 1.1
% Purpose : Using built-in matlab editor, create a matlab file for the function
% [w,min_w] = rectify(x,selcet)
% that takes an arbitrary 1-D signal array x and creates a rectified 1-d signal array w
% in which either full-wave rectification
% w[n]=-|x[n]|, for all n
% is applied if variable select is assigned 1, or half-wave rectification
% w[n]=x[n], if x<0
% w[n]=0, else
% is applied if variable select is set to 2. In addition to the function output of array w,
% the minimimun value min_w within array w should also be returned by the function.
% No for or other looping constructs should be used to create this function.
% Comment lines should be added at the top of the function listing to define the purpose of
% the function and to describe the various input and output variables.
% Provide a listing printout of your function file
%
% To comment : press "CTRL+R"
% To uncomment : press "CTRL+T"

function[w,min_w] = rectify(x,select)
if select==1
w=-abs(x); %full-wave rectification
elseif select==2
temp=x<0; %half-wave rectification
w=x.*temp;

% temp=0.5*(sign(x)+1); %alternative method to get half-wave
% w=x.*temp; %rectification

else
w=0;
end

min_w=min(w);

09. 1. 6 11:30 C:\Users\\Documents\MATLAB\351\demo_rectify.m 1 of 3
% ECE 351 matlab exercise 1.2
% Purpose : Using the built-in matlab editor, create a matlab script demo_recify.m
% that performs steps (a) through (g) below avoiding use of for or other looping
% constructs. Include a listing of the script file and a printout of a single
% figure with six(6) panels [arranged 3 rows x 2 colunms with the subplot directive]
% that the script will generate

clear all % remove all variables, globals, functions and links from memory
close all % closes all the open figure windows.

% (a) generate an array t of sample times with total duration T seconds.
% (you will have to select an appropriate value of T) that covers approximately
% 5 cycles of a 2000 Hz sinusoid with at least 40 time samples per cycle.
% (use 3-term colon notation to define t with single line of matlab code)

f=2000; % Hz
T=5*1/f; % sec
% t=[0;T/40;T];

% (b) generate a 2KHz cosine signal 5*cos(2*pi*2000*t) by sampleing within the time
% range -T/2 to T/2

t=[-T/2:T/40:T/2];
x=5*cos(2*pi*2000*t);

% (c) In the top two panels of the figure generated by the script, plot x in the left
% panel and plot the half-wave negative rectification of X in the right panel. Lable
% the horizontal axis of each with the text "Time(sec)" and the vertical axis of each
% with "Amplitude(V)". Show the title of the left-hand plot as "COSINE SIGNAL" and
% the title of the right-hand plot as "HALF-WAVE RECTIFIED SIGNAL". Select "TEXT"
% under the "INSERT" menu in the figure window to add the text "rect_min=__" to
% the figure using the actual value of min_w from the rectification function in
% the blank space

[x_half, min_x]=rectify(x,2);

subplot(3,2,1); % SUBPLOT(m,n,p) breaks the figure window into an m-by-n matrix of
% small axes, select the p-th axes for the current plot,
% and returns the axis handle. The axes are counted along the
% top row of the figure window, the the second row, etc.

plot(t,x); % PLOT(x,y) plots vector y versus vector x.
grid; % GRID, by itself, toggles the major grid lines of the current axes.
title('COSINE SIGNAL'); % TITLE('text') adds text at the top of the current axis.
xlabel('Time(sec)'); % XLABEL('text') adds text beside the X-axis on the current axis.
ylabel('Amplitude(V)'); % YLABEL('text') adds text beside the Y-axis on the current axis.

09. 1. 6 11:30 C:\Users\\Documents\MATLAB\351\demo_rectify.m 2 of 3
subplot(3,2,2);
plot(t,x_half);
grid;
title('HALF-WAVE RECTIFIED SIGNAL');
xlabel('Time(sec)');
ylabel('Amplitude(V)');

% Generate a 3V DC-offset sinusoidal signal y=x+3.

y=x+3;

% (e)In the middle two panels of the script-generated figure, plot y in the left
% panel and plot the full-wave negative rectification of y in the right panel.
% Use the horizontal and vertical labeling of (c). Show the title of the left
% -hand plot as "DC-OFFSET COSINE SIGNAL" and the title of the right-hand plot
% as "FULL-WAVE RECTIFIED SIGNAL". Select TEXT under the INSERT menu in the figure
% window to add the text "rect_min=__" to the figure using the actual value of min_w
% from the rectification function in the blank space.

[y_full, min_y]=rectify(y,1);

subplot(3,2,3);
plot(t,y);
grid;
title('DC-OFFSET COSINE SIGNAL');
xlabel('Time(sec)');
ylabel('Amplitude(V)');

subplot(3,2,4);
plot(t,y_full);
grid;
title('FULL-WAVE RECTIFIED SIGNAL');
xlabel('Time(sec)');
ylabel('Amplitude(V)');

% (f) Generate a -1.3V DC-offset sinusoidal signal z=x-1.3.

z=x-1.3;

% (g) In the bottom two panels of the script-generated figure, plot z in the
% left panel and plot the half-wave negative rectification of z in the right
% panel. Use the horizontal and vertical labeling of (c). Show the title of the
% left-hand plot as "DC-OFFSET SINISOIDAL" and the title of the right-hand plot
% as "HALF-WAVE RECTIFICATION". Select TEXT under the INSERT menu in the figure
% window to add the text "rect_min=__" to the figure using the actual value of
% minimun value in the blank space.

09. 1. 6 11:30 C:\Users\\Documents\MATLAB\351\demo_rectify.m 3 of 3
[z_half, min_z]=rectify(z,2);

subplot(3,2,5);
plot(t,z);
grid;
title('DC-OFFSET SINUSOIDAL');
xlabel('Time(sec)');
ylabel('Amplitude(V)');

subplot(3,2,6);
plot(t,z_half);
grid;
title('HALF-WAVE RECTIFICATION');
xlabel('Time(sec)');
ylabel('Amplitude(V)');

-1.5 -1 -0.5 0 0.5 1 1.5
x 10
-3
-5
0
5
COSINE SIGNAL
Time(sec)
A
m
p
l
i
t
u
d
e
(
V
)
-1.5 -1 -0.5 0 0.5 1 1.5
x 10
-3
-6
-4
-2
0
HALF-WAVE RECTIFIED SIGNAL
Time(sec)
A
m
p
l
i
t
u
d
e
(
V
)
-1.5 -1 -0.5 0 0.5 1 1.5
x 10
-3
-2
0
2
4
6
8
DC-OFFSET COSINE SIGNAL
Time(sec)
A
m
p
l
i
t
u
d
e
(
V
)
-1.5 -1 -0.5 0 0.5 1 1.5
x 10
-3
-8
-6
-4
-2
0
FULL-WAVE RECTIFIED SIGNAL
Time(sec)
A
m
p
l
i
t
u
d
e
(
V
)
-1.5 -1 -0.5 0 0.5 1 1.5
x 10
-3
-8
-6
-4
-2
0
2
4
DC-OFFSET SINUSOIDAL
Time(sec)
A
m
p
l
i
t
u
d
e
(
V
)
-1.5 -1 -0.5 0 0.5 1 1.5
x 10
-3
-8
-6
-4
-2
0
HALF-WAVE RECTIFICATION
Time(sec)
A
m
p
l
i
t
u
d
e
(
V
)
rect-min=-5
rect-min=-8
rect-min=-6.3

You might also like