0% found this document useful (0 votes)
37 views14 pages

MATLAB Simulation of Time Signals

The document describes a MATLAB code that simulates various continuous and discrete time signals. It allows the user to generate sine, cosine, unit impulse, unit step, ramp, and exponential signals. It also defines two custom signals and plots their continuous and discrete time representations. The code takes user input to select the desired signal type and format. It uses functions like unit_step and ramp to define the custom signals.

Uploaded by

LUKESH ankamwar
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)
37 views14 pages

MATLAB Simulation of Time Signals

The document describes a MATLAB code that simulates various continuous and discrete time signals. It allows the user to generate sine, cosine, unit impulse, unit step, ramp, and exponential signals. It also defines two custom signals and plots their continuous and discrete time representations. The code takes user input to select the desired signal type and format. It uses functions like unit_step and ramp to define the custom signals.

Uploaded by

LUKESH ankamwar
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

DSP LAB 1

Aim: To Simulate the Generation of Continuous Time and Discrete Time


Signals.

Software: MATLAB

Group Number 1:
1. Omkar Bhilare 191060901
2. Omkar Sargar 181060047
3. Om Fuke 181060046
4. Lukesh Ankamwar 181060038

Code:
clc;
clear all;
x =[-40:.01:40];
no = input('Enter the wave you want\n1 : Sine Wave \n2 : Cos
Wave\n3 : Unit Impulse Function\n4 : Unit Step Function\n5 :
Ramp function\n6 : Exponential function\n');

switch no
case 1
z = input('Input the type of signal\n1 : Continuous
\n2 : Discrete\n');
amp = input("Enter the amplitude of the signal: ");
fc = input("Enter the frequency of the signal: ");
y = amp*sin(2*pi*x*1/fc);
%subplot(2,1,1);
if z ==1
plot(x,y);grid on;
xlabel('Time t');
ylabel('x(t)');
grid on;
title('Continuous Sine Signal');
elseif z ==2
stem(x,y);grid on;
xlabel('Time n');
ylabel('x[n]');
grid on;
title('Discrete Sine Signal');
else
display("Wrong input for type of signal");
end

case 2
z = input('Input the type of signal\n1 : Continuous
\n2 : Discrete\n');
amp = input("Enter the amplitude of the signal: ");
fc = input("Enter the frequency of the signal: ");
y = amp*cos(2*pi*x*1/fc);
if z ==1
plot(x,y);grid on;
xlabel('Time t');
ylabel('x(t)');
grid on;
title('Continuous Cos Signal');
elseif z ==2
stem(x,y);grid on;
xlabel('Time n');
ylabel('x[n]');
grid on;
title('Discrete Cos Signal');
else
display("Wrong input for type of signal");
end

case 3

N=15;
x=-N:1:N;
y=[zeros(1,N),ones(1,1),zeros(1,N)];
z = input('Input the type of signal\n1 : Continuous
\n2 : Discrete\n');
if z ==1
plot(x,y);grid on;
xlabel('Time t');
ylabel('x(t)');
grid on;
title('Continuous Unit Impulse Signal ');
elseif z ==2
stem(x,y);grid on ;
xlabel('Time n');
ylabel('x[n]');
grid on;
title('Discrete Unit Impulse Signal');
else
display("Wrong input for type of signal");
end
case 4
N=20;
x=0:1:N-1;
y=ones(1,N);
z = input('Input the type of signal\n1 : Continuous
\n2 : Discrete\n');
if z ==1
plot(x,y); grid on;
plot(x,y);grid on;
xlabel('Time t');
ylabel('x(t)');
grid on;
title('Continuous Unit Impulse Signal');
elseif z ==2
stem(x,y);grid on;
xlabel('Time n');
ylabel('x[n]');
grid on;
title('Discrete Unit Impulse Signal');
else
display("Wrong input for type of signal");
end

case 5
N=20;
x=-N:1:N-1;
amp = input("Enter the amplitude of the signal: ");
y=amp*x.*[x>=0];
z = input('Input the type of signal\n1 : Continuous
\n2 : Discrete\n');
if z ==1
plot(x,y); grid on;
plot(x,y);grid on;
xlabel('Time t');
ylabel('x(t)');
grid on;
title('Continuous Unit Ramp Signal');
elseif z ==2
stem(x,y);grid on;
xlabel('Time n');
ylabel('x[n]');
grid on;
title('Discrete Unit Ramp Signal');
else
display("Wrong input for type of signal");
end
case 6
N=10;
x=0:0.5:N;
amp = input("Enter the amplitude of the signal: ");
y=exp(amp*x);
z = input('Input the type of signal\n1 : Continuous
\n2 : Discrete\n');
if z ==1
plot(x,y); grid on;
plot(x,y); grid on;
plot(x,y);grid on;
xlabel('Time t');
ylabel('x(t)');
grid on;
title('Continuous Exponential Signal');
elseif z ==2
stem(x,y);grid on;
xlabel('Time n');
ylabel('x[n]');
grid on;
title('Discrete Exponential Signal');
else
display("Wrong input for type of signal");
end

otherwise
disp('Wrong input');
end

%%
clc;
clear all;
t = -5:0.01:5;
t1 = -5:0.1:5;

Graph1_continuous = unit_step(t)+unit_step(t-1)-unit_step(t-
2)-unit_step(t-3);
Graph1_discrete = unit_step(t1)+unit_step(t1-1)-unit_step(t1-
2)-unit_step(t1-3);

z = input('Input the type of signal\n1 : Continuous \n2 :


Discrete\n');
if z==1
plot(t, Graph1_continuous)
xlabel('Time t');
ylabel('x(t)');
grid on;
title('Continuous Time Customized Signal 1');
else if z ==2
stem(t1, Graph1_discrete)
xlabel('Time n');
ylabel('x[n]');
grid on;
title('Discrete Time Customized Signal 1');
end
end

%%
clc;
clear all;
t = -5:0.01:5;
t1 = -5:0.1:5;

Graph2_continuous = -unit_step(t+3) +
2*unit_step(t+2)+ramp(t+1)-...
2*ramp(t)+ramp(t-1)-2*unit_step(t-2)+unit_step(t-3);
Graph2_discrete = -unit_step(t1+3) +
2*unit_step(t1+2)+ramp(t1+1)-...
2*ramp(t1)+ramp(t1-1)-2*unit_step(t1-2)+unit_step(t1-3);
z = input('Input the type of signal\n1 : Continuous \n2 :
Discrete\n');
if z==1
plot(t, Graph2_continuous)
xlabel('Time t');
ylabel('x(t)');
grid on;
title('Continuous Time Customized Signal 2');
ylim([-3,3])
else if z ==2
stem(t1, Graph2_discrete)
xlabel('Time n');
ylabel('x[n]');
grid on;
title('Discrete Time Customized Signal 2');
ylim([-3,3])
end
end

function out = unit_step(t)


x1 = 1;
x0 = 0;
out = x1.*(t>=0) + x0.*(t<0);
end

function out = ramp(t)


x1=t;
x0=0;
out = x1.*(t>=0) + x0.*(t<0);
end
Outputs:
Conclusion: Continuous Time and Discrete Time Signals were simulated in
MATLAB successfully. The code written is capable of simulating sine, cosine
unit impulse, unit step, ramp and exponential signals both in continuous and
discrete time. Also, custom signals can also be simulated.

Common questions

Powered by AI

The choice between continuous and discrete signals significantly affects output visualization in MATLAB. Continuous signals are plotted using the 'plot' function, which results in a smooth graph, ideal for signals like sine, cosine, and exponential where data continuity is key . Discrete signals, on the other hand, use the 'stem' function, displaying data as individual points, which is critical for signals where discrete data points are meaningful, like the unit impulse and customized discrete time signals . The difference is crucial when accuracy in data representation is needed in discrete data analysis or when a theoretical understanding of signal continuity is required .

In MATLAB, the unit impulse function is represented in continuous form using 'plot' with continuous axes and singularity in signal magnitude, while in discrete form, it uses 'stem' to represent distinct data points at specific intervals . This difference in representation allows for application-specific modeling; continuous forms are ideal for theoretical analysis in integrating system responses, while discrete forms are suited for digital applications, like filters in real-time processing, demonstrating how representation directly affects usage and interpretation in signal processing tasks .

In the simulation of waveform signals in MATLAB, 'amp' is used to scale the signal amplitude, directly affecting the signal's vertical size and power. It modifies the height of the waveform peaks, which changes the signal's maximum and minimum values, directly impacting measurements like signal power and peak-to-peak amplitude . An increased 'amp' results in a more pronounced signal with larger peaks, influencing applications requiring different amplitude signals .

The customized signal 'Graph1' is constructed by combining multiple unit step functions at different times. It uses operations to add and subtract these steps, specifically 'unit_step(t) + unit_step(t-1) - unit_step(t-2) - unit_step(t-3)' for continuous signals . This construction enables the creation of complex signals that can represent a sequence of steps in signal processing, illustrating how simple functions can be combined to synthesize more meaningful and complex signal forms, suitable for various analytical applications .

The MATLAB function 'ramp' generates linearly increasing signals that start from zero when the input time or index is non-negative, effectively serving as a foundational component in signal processing to model signals that linearly change over time . This function is crucial for simulating processes or components that exhibit linear behaviors, such as slowly ramping inputs to system circuits to prevent surges, and in creating triangular waveforms, crucial in both analog and digital signal processing for filtering and waveform synthesis .

In signal modeling and simulation, the exponential function serves as a critical component in representing signals with exponential growth or decay patterns. The MATLAB code 'y = exp(amp*x)' generates these signals by applying an exponential operation across a range of time or index values, modulated by an amplitude factor . This function is pivotal when simulating real-world phenomena such as growth processes, decay systems, and systems responses that follow an exponential trend . Its role is to model the dynamics of signals more accurately in certain applications where linear approaches are insufficient .

The MATLAB code utilizes a 'switch' statement to handle different types of signal simulations, which allows for organized and clear structure when selecting between various signals . While this approach is functional, it could be optimized by encapsulating individual signal generation logic into separate functions, which would enhance code modularity and reusability. Additionally, employing more dynamic input handling and validation would improve robustness and usability . Such improvements would streamline the code, making it easier to extend and maintain in larger projects or when adding new signal types .

In MATLAB, the 'unit_step' function differentiates between zero and positive signal values using logical operations. It multiplies 'x1', which is equal to 1, with values of 't' greater than or equal to 0, and 'x0', which is equal to 0, with values of 't' less than 0 . This conditional logic effectively models the step function behavior .

To simulate sine signals in MATLAB, the code differs between continuous and discrete signals primarily in the plotting function used. For continuous signals, the 'plot' function is used, specifying 'Time t' and 'x(t)' as labels, whereas for discrete signals, the 'stem' function is used, labeling the axes as 'Time n' and 'x[n]' . In both cases, the sine wave is generated using 'amp*sin(2*pi*x*1/fc)', where 'amp' is the amplitude and 'fc' is the frequency .

The 'clear all' command in MATLAB removes all variables from the workspace, freeing up system memory and avoiding conflicts with previously stored data, which ensures that each execution of the simulation script starts with a clean slate. The 'clc' command clears the command window, removing previous outputs and creating a more readable output space . These commands are vital in maintaining a clean working environment, facilitating more accurate debugging and testing, and preventing unexpected errors due to residual variable states .

You might also like