SIGNALS PROJECT
Awab Elsadig Mohamed Ali – 21011819
Mark Marei Eissa – 21011024
Arsany Waheed Fahmy – 21010228
Omar Miftah Abdulsalam Ataya – 18011165
General Signal Generator
Code:
% Enter the sampling frequency
samplingFrequency = input("Enter the Sampling Frequency : ");
if(~isnumeric(samplingFrequency) || samplingFrequency <= 0)
error("Sampling Frequency must be a positive number.");
end
% Enter the starting time
startTime = input('Enter the Starting Time: ');
if(~isnumeric(startTime))
error("Starting Time must be a number.");
end
% Enter the ending time
endTime = input('Enter the Ending Time: ');
if(~isnumeric(endTime))
error("Ending Time must be a number.");
end
% Enter the number of breakpoints
numBreakpoints = input('Enter the Number Of Breakpoints: ');
if(~isnumeric(numBreakpoints) || numBreakpoints < 0)
error("Number Of Breakpoints must be a positive number.");
end
% Enter the positions of breakpoints
breakPositions = zeros(1, numBreakpoints);
for i = 1:numBreakpoints
breakPositions(i) = input("Enter the Position Number: " + i + " ");
if(~isnumeric(breakPositions(i)))
error("Position must be a number.");
end
end
breakPositions = [startTime breakPositions endTime];
signals = [];
figure;
grid;
for i = 1:numBreakpoints+1
% Display the menu and prompt user choice
fprintf("Segment %d: From %.2f to %.2f\n", i, breakPositions(i),
breakPositions(i+1));
choices = {'DC signal', 'Ramp signal', 'General order polynomial', 'Exponential
signal', 'Sinusoidal signal', 'Sinc function', 'Triangle pulse'};
choice = menu('Select the type of signal', choices);
% Switch case based on user choice
switch choice
case 1 % DC signal
amplitude = input("Enter the Amplitude of DC Signal: ");
t = linspace(breakPositions(i), breakPositions(i+1),
(breakPositions(i+1)-breakPositions(i)) * samplingFrequency);
signal = ones(1, (breakPositions(i+1)-breakPositions(i)) *
samplingFrequency) * amplitude;
plot(t, signal, 'b-', 'LineWidth', 1)
hold on;
case 2 % Ramp signal
slope = input("Enter the Slope Of Ramp Signal: ");
intercept = input("Enter the Intercept Of Ramp Signal: ");
t = linspace(breakPositions(i), breakPositions(i+1),
(breakPositions(i+1)-breakPositions(i)) * samplingFrequency);
signal = slope * t + intercept;
plot(t, signal, 'b-', 'LineWidth', 1)
hold on;
case 3 % General order polynomial
amplitude = input("Enter the Amplitude Of General Order Polynomial: ");
power = input("Enter the Power Of General Order Polynomial: ");
intercept = input("Enter the Intercept Of General Order Polynomial: ");
t = linspace(breakPositions(i), breakPositions(i+1),
(breakPositions(i+1)-breakPositions(i)) * samplingFrequency);
signal = amplitude * (t.^power) + intercept;
plot(t, signal, 'r-', 'LineWidth', 1)
hold on;
case 4 % Exponential signal
amplitude = input("Enter the Amplitude Of Exponential Signal: ");
exponent = input("Enter the Exponent Of Exponential Signal: ");
t = linspace(breakPositions(i), breakPositions(i+1),
(breakPositions(i+1)-breakPositions(i)) * samplingFrequency);
signal = amplitude * exp(exponent * t);
plot(t, signal, 'g-', 'LineWidth', 1)
hold on;
case 5 % Sinusoidal signal
amplitude = input("Enter the Amplitude Of Sinusoidal Signal: ");
frequency = input("Enter the Frequency Of Sinusoidal Signal: ");
phase = input("Enter the Phase of Sinusoidal Signal: ");
t = linspace(breakPositions(i), breakPositions(i+1),
(breakPositions(i+1)-breakPositions(i)) * samplingFrequency);
signal = amplitude * sin((2*pi*frequency*t) + phase);
plot(t, signal, 'y-', 'LineWidth', 1)
hold on;
case 6 % Sinc function
amplitude = input("Enter the Amplitude of Sinc Function: ");
centerShift = input("Enter the Center Shift of Sinc Function: ");
t = linspace(breakPositions(i), breakPositions(i+1),
(breakPositions(i+1)-breakPositions(i)) * samplingFrequency);
signal = amplitude * sinc(t-centerShift);
plot(t, signal, 'g-', 'LineWidth', 1)
hold on;
case 7 % Triangle pulse
amplitude = input("Enter the Amplitude of Triangle Signal: ");
centerShift = input("Enter the Center Shift Of Triangle Signal: ");
width = input("Enter the Width Of Triangle Signal: ");
t = linspace(breakPositions(i), breakPositions(i+1),
(breakPositions(i+1)-breakPositions(i)) * samplingFrequency);
signal = amplitude * sawtooth(2*pi*(t-centerShift)/width, 0.5);
plot(t, signal, 'g-', 'LineWidth', 1)
hold on;
end
if i == (numBreakpoints+1)
hold off;
end
f = [signals signal];
signals = f;
end
t = linspace(startTime, endTime, (endTime-startTime)*samplingFrequency);
while true
options = {'Amplitude Scaling', 'Time reversal', 'Time shift', 'Expanding the
signal', 'Compressing the signal', 'Clipping the signal', 'The first derivative of
the signal', 'Exit'};
choice = menu('Select the signal processing operation', options);
% Switch case based on user choice
switch choice
case 1 % Amplitude Scaling
scalingFactor = input("Enter the Scaling Factor: ");
signals = f * scalingFactor;
plot(t, signals, 'b-', 'LineWidth', 1)
f = signals; % Update f
case 2 % Time reversal
t = -t; % Invert the time vector
plot(t, f, 'b-', 'LineWidth', 1)
case 3 % Time shift
shiftAmount = input("Enter the Shift Amount: ");
numSamples = (endTime - startTime) * samplingFrequency;
if shiftAmount >= 0
% Positive shift: shift signal to the right
numZeros = round(shiftAmount * samplingFrequency);
signals = [zeros(1, numZeros), f(1:end-numZeros)];
else
% Negative shift: shift signal to the left
numZeros = round(abs(shiftAmount) * samplingFrequency);
signals = [f(numZeros+1:end), zeros(1, numZeros)];
end
% Update the time vector
t = linspace(startTime, endTime, numSamples);
% Plot the shifted signal
plot(t, signals, 'r-', 'LineWidth', 1)
% Update f
f = signals;
case 4 % Expanding the signal
expandFactor = input("Enter the Expanding Factor: ");
t = t * expandFactor;
% Plot the expanded signal
plot(t, signals, 'r-', 'LineWidth', 1)
f = signals; % Update f
case 5 % Compressing the signal
compressionFactor = input("Enter the Compression Factor: ");
t = t / compressionFactor;
% Plot the compressed signal
plot(t, signals, 'g-', 'LineWidth', 1)
f = signals; % Update f
case 6 % Clipping the signal
minClip = input("Enter the Minimum Clipping: ");
maxClip = input("Enter the Maximum Clipping: ");
signals = f;
signals(f < minClip) = minClip;
signals(f > maxClip) = maxClip;
plot(t, signals, 'g-', 'LineWidth', 1)
f = signals; % Update f
case 7 % The first derivative of the signal
% Calculate the first derivative of the signal
signals = diff(f);
% Update the time vector to match the reduced number of samples
t = t(1:end-1); % Remove the last element of the time vector
% Plot the first derivative of the signal
plot(t, signals, 'g-', 'LineWidth', 1)
% Update f
f = signals;
case 8 % Exit loop
break; % Exit the loop
end
hold off;
end
Simulation: Menu to choose signals:
Enter the Sampling Frequency :
1000
Enter the Starting Time:
0
Enter the Ending Time:
25
Enter the Number Of Breakpoints:
4
Enter the Position Number: 1
5
Enter the Position Number: 2
10
Enter the Position Number: 3
15
Enter the Position Number: 4
20
Segment 1: From 0.00 to 5.00
Enter the Slope Of Ramp Signal:
2
Enter the Intercept Of Ramp Signal:
0
Segment 2: From 5.00 to 10.00 Menu to choose operaton:
Enter the Amplitude of DC Signal:
13
Segment 3: From 10.00 to 15.00
Enter the Amplitude of Sinc Function:
55
Enter the Center Shift of Sinc Function:
12.5
Segment 4: From 15.00 to 20.00
Enter the Amplitude of Triangle Signal:
4
Enter the Center Shift Of Triangle Signal:
17.5
Enter the Width Of Triangle Signal:
1
Segment 5: From 20.00 to 25.00
Enter the Amplitude Of Sinusoidal Signal:
4
Enter the Frequency Of Sinusoidal Signal:
2
Enter the Phase of Sinusoidal Signal:
45
Outputs:
Main Signal
^ Amplitude Scale ^
^ Clipping the Signal ^
^ Expanding the Signal ^
^ Compressing the Signal ^
^ Time Shift ^
^ Time Reversal ^