You are on page 1of 3

% defining parameters

frequency = 1;
time_period = 1/frequency;
num_cycle = 3;
duration = num_cycle/frequency;
amplitude = 1;
sample = 10000;
t = linspace(0, duration, duration * sample);
%Generating a sin function
sin_wave = amplitude*sin(2*pi*frequency*t);
plot(t, sin_wave,'linewidth',2);
title('Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude(m)');
legend ('sin wave')
grid on;
///////////////////////////////////////
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^?////////////////////////
witn linspace command
% Define parameters
frequency = 1;
amplitude = 1;
max_cycles = 100;
sampling_rate = 1000;
duration = max_cycles / frequency;
t_original = linspace(0, duration, duration * sampling_rate);
original_signal = amplitude * sin(2 * pi * frequency * t_original);
% Extract the first three cycles
Extract_cycle = 3;
Extracting_duration = Extract_cycle / frequency;
Extracted_time = linspace(0, Extracting_duration, Extracting_duration *
sampling_rate);
extracted_signal = amplitude * sin(2 * pi * frequency * Extracted_time);
% Plot the original and extracted signals
subplot(2, 1, 1);
plot(t_original, original_signal,'linewidth',2);
title('Original Signal (100 Cycles)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('orignal signal')
grid on;

subplot(2, 1, 2);
plot(Extracted_time, extracted_signal,'--r','linewidth',2);
title('Extracted Signal (3 Cycles)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Extracted signal')
grid on;

witn xlin command


% Define parameters
frequency = 1; % Frequency of the original sine wave in Hz
amplitude = 1; % Amplitude of the original sine wave
total_cycles = 100; % Total number of cycles in the original signal
sampling_rate = 1000; % Sampling rate in Hz
duration = total_cycles / frequency; % Total duration of the original signal

% Generate time vector for the original signal


t_original = linspace(0, duration, duration * sampling_rate);

% Generate the original sine wave with 100 cycles


original_signal = amplitude * sin(2 * pi * frequency * t_original);

% Extract the first three cycles


extracted_signal = amplitude * sin(2 * pi * frequency * t_original);

% Plot the original and extracted signals


subplot(2, 1, 1);
plot(t_original, original_signal,'g','linewidth',2);
title('Original Signal (100 Cycles)');
xlabel('Time (s)');
ylabel('Amplitude(m)');
legend('t original')
grid on;

subplot(2, 1, 2);
plot(t_original, extracted_signal,':g','linewidth',2);
xlim([0 3])
title('Extracted Signal (3 Cycles)');
xlabel('Time (s)');
ylabel('Amplitude(m)');
legend('t extracted')
grid on;
//////////////////////////////////
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&7777777777777777777777777777777777777
in LAB TASK 03
warning Integer operands are required for colon operator when used as index
to remove this we use I've rounded the indices in the frequencies vector
calculation using round(N/2) to ensure that they are integers, which will remove
the warning.
% Define parameters
Fs = 10000; % Sampling frequency (Hz)
t = 0:1/Fs:1; % Time vector from 0 to 1 second
frequencies = [500, 2500, 4000]; % Frequencies of the three components (Hz)
A_initial = 1; % Initial equal amplitude for all components

% Generate the individual sine waves with equal amplitudes


signals = A_initial * sin(2 * pi * frequencies' * t);

% Create the composite signal by adding the individual components


composite_signal = sum(signals);

% Compute the frequency spectrum using FFT


N = length(composite_signal);
frequencies = linspace(0, Fs/2, round(N/2) + 1); % Corrected frequency axis
spectrum = abs(fft(composite_signal)/N); % Magnitude spectrum

% Plot the composite signal


figure;
subplot(2, 1, 1);
plot(t, composite_signal);
title('Composite Signal (Equal Amplitudes)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Plot the frequency spectrum


subplot(2, 1, 2);
plot(frequencies, 2*spectrum(1:round(N/2)+1)); % Corrected index range
title('Frequency Spectrum (Equal Amplitudes)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% Adjust the subplot layout


sgtitle('Composite Signal and Frequency Spectrum (Equal Amplitudes)');
??????????????????????????????????????????????
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&???????????????????????
in lab task 04
flow = 500; %cutoff for low-pass
fhigh = 2500; %cutoff for high-pass
fband = [500 4000]; %cutoff for band-pass
fs = 25e3;
n = 6; %order of filter
[bl,al] = butter(n,flow/(fs/2));
figure(1); freqz(bl,al); title('Low Pass')
[bh,ah] = butter(n,fhigh/(fs/2), 'high');
figure(2); freqz(bh,ah); title('High Pass')
[bp,ap] = butter(n/2,fband/(fs/2), 'bandpass'); %note order for bandpass and
bandstop will be n/2
figure(3); freqz(bp,ap); title('Band Pass')
?????????????????????????????????*************************?????????????????????????
in lab task 05

You might also like