PCS Lab 1
PCS Lab 1
Lab # 01:
Lab Assessment
Post Lab Total
In-Lab Data
Data Analysis Writing Style
Presentation
Page 2 of 11
Output:-
In Lab Task # 02
Analyze the frequency spectrum of the signal generated in task 1.1, with 100 cycles instead
of 5. However, show only 3 cycles in the figure (use xlim command).
Code in Matlab:-
frequency = 1000;
total_cycles = 100;
sampling_rate = 10 * frequency;
duration = total_cycles / frequency;
t = 0:1/sampling_rate:duration;
sine_wave = sin(2 * pi * frequency * t);
subplot(2,1,1)
plot(t, sine_wave,'g','linewidth',2);
xlabel('Time (s)');
ylabel('Amplitude');
title('1000 Hz Sine Wave with 100 Cycles');
grid on;
subplot(2,1,2)
plot(t, sine_wave,':g','linewidth',2);
xlabel('Time (s)');
ylabel('Amplitude');
title('1000 Hz Sine Wave with 100 Cycles');
grid on;
xlim([0, 3 / frequency]);
Output:-
In Lab Task # 03
Make a composite signal with sin wave of three frequencies: f1 = 500Hz, f2 = 2500Hz, f3 =
4000Hz. Assume equal amplitudes initially to analyze the frequency spectrum and change the
amplitudes of the three waves and describe the changes in the spectrum of composite signal.
Code in MATLAB:-
Fs = 10000;
t = 0:1/Fs:1;
frequencies = [500, 2500, 4000];
Amplitude = 1;
signals = Amplitude *sin(2. *pi *frequencies' *t);
composite_signal = sum(signals);
N = length(composite_signal);
frequencies = linspace(0, Fs/2, round(N/2) + 1);
spectrum = abs(fft(composite_signal)/N);
figure;
subplot(2, 1, 1);
plot(t, composite_signal);
title('Composite Signal (Equal Amplitudes)');
xlabel('Time (s)');
ylabel('Amplitude');
legend('composite function')
grid on;
subplot(2, 1, 2);
plot(frequencies, 2*spectrum(1:round(N/2)+1),'linewidth',2);
title('Frequency Spectrum (Equal Amplitudes)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
legend('freq spectrum')
grid on;
In Lab Task # 04
Make a low-pass, band-pass and high-pass Butterworth filter of order 6 with following cutoff
frequencies:- Lowpass cutoff: 500Hz, High pass cutoff: 2500Hz, Bandpass cutoff: 500Hz-
4000Hz Visualize the filter magnitude and phase response and highlight the magnitude
response at the cutoff frequencies(use data cursor).
Code In MATLAB:-
flow = 500;
fhigh = 2500;
fband = [500 4000];
fs = 25e3;
n = 6;
[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');
figure(3); freqz(bp,ap); title('Band Pass')
Output:-
In Lab Task # 05
Use appropriate filters with appropriate cutoff frequencies to extract signal components from
composite signal in task 1.3. Plot Original signal with spectrum, and the extracted
components with respective spectra.
Code in MATLAB:-
Fs = 5000;
t = 0:1/Fs:1;
f1 = 500;
f2 = 2500;
f3 = 4000;
A = 1;
s1 = A*sin(2*pi*f1*t);
s2 = A*sin(2*pi*f2*t);
s3 = A*sin(2*pi*f3*t);
composite_signal = s1 + s2 + s3;
cutoff_lowpass = 150;
filter_order = 100;
lowpass_filter = designfilt('lowpassfir', 'FilterOrder', filter_order,
'CutoffFrequency', cutoff_lowpass, 'SampleRate', Fs);
cutoff_bandpass_low = 150;
cutoff_bandpass_high = 250;
bandpass_filter = designfilt('bandpassfir', 'FilterOrder', filter_order,
'CutoffFrequency1', cutoff_bandpass_low, 'CutoffFrequency2',
cutoff_bandpass_high, 'SampleRate', Fs);
cutoff_highpass = 2000;
highpass_filter = designfilt('highpassfir', 'FilterOrder', filter_order,
'CutoffFrequency', cutoff_highpass, 'SampleRate', Fs);
signal_lowpass = filter(lowpass_filter, composite_signal);
signal_bandpass = filter(bandpass_filter, composite_signal);
signal_highpass = filter(highpass_filter, composite_signal);
N = length(composite_signal);
frequencies = linspace(0, Fs/2, floor(N/2) + 1);
spectrum_original = abs(fft(composite_signal)/N);
spectrum_lowpass = abs(fft(signal_lowpass)/N);
spectrum_bandpass = abs(fft(signal_bandpass)/N);
spectrum_highpass = abs(fft(signal_highpass)/N);
figure;
subplot(4, 1, 1);
plot(t, composite_signal,'linewidth',2);
title('Original Composite Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(4, 1, 2);
plot(frequencies, 2*spectrum_original(1:floor(N/2)+1),'linewidth',2);
title('Original Signal Spectrum');
xlabel('Frequency (Hz)');
Page 8 of 11
ylabel('Magnitude');
grid on;
subplot(4, 1, 3);
plot(t, signal_lowpass,'linewidth',2);
title('Low-Pass Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(4, 1, 4);
plot(frequencies, 2*spectrum_lowpass(1:floor(N/2)+1),'linewidth',2); %
Output:-
Conclusion:-
In this lab we learned how to use the basic cammands of matlab
We learned how to plot different signals
We learned how to create a sin wave and generate its graph
We extrected 3 cycles from 100 cycles using xlim cammand
We learned how to create a composite function and its frequency spectrum with cutoff
frequency
We also learned about low pass,high pass and band pass and its phases