GuanSe
GuanSe

Reputation: 39

How do I increase the number of samples plotted?

Firstly, I'm not entirely sure whether what I'm doing is correct hahha. This could also be a problem of my understanding of the concepts.

These are the instructions in our lab activity:

1.Create a sinusoid (sin 2pi50*t) with an amplitude of (SeatNo *0.1) with a sampling frequency of:

a. 8000 Hz and plot the graph to show up to 200 samples.

c. 4050 Hz and plot the graph to show up to 200 samples.

This is my attempt at that:

amp = 12*0.1; %amplitude
%%%%%%%%%%%%%%%%%%%%
%     Number 1     %
%%%%%%%%%%%%%%%%%%%%
%sinusoidal wave
figure(1);
n = 200; %samples

%plot a
subplot(2,2,1);
fs = 8000; %sampling frequency
a = num1(fs, amp, n);
plot(a);
title('Plot 1a');

%plot c
subplot(2,2,3);
fs = 4050; %sampling frequency
c = num1(fs, amp, n);
plot(c);
title('Plot 1c');

%%%%%%%%%%%%%%%%%%%%
%     Number 1     %
%%%%%%%%%%%%%%%%%%%%
function y = num1(fs, amp, n)
%solves for the sinusoidal signal using the given
    t = (1/fs)*n;
    t = linspace(0, numSec, n);
    y = amp*sin(2*pi*50*t);
end

Now here's what I don't understand how to do:

2.Compare the length of signals in 1a and 1c. If required, perform zero-padding at the end of the shorter signal so that they will have the same length and

a. Add the two signals and plot 250 samples.

I already have sigadd function, and have added the signals but it's 200 samples. How do I make it plot 250 samples?

Upvotes: 1

Views: 537

Answers (2)

MichaelTr7
MichaelTr7

Reputation: 4757

Looks like this assignment is created to distinguish the difference between summing up signals with respect to time or sample index. Here are some graphs that I came up that use similar resampling techniques to achieve the 250 sample length required for the cumulative signal.

Summation with Respect to Sample Index:

Respect to Sample Index

clf;
%Configuring Signal 1%
Sampling_Frequency = 8000;
Number_Of_Samples = 200;
Amplitude = 12*0.1;

Sampling_Period = 1/Sampling_Frequency;
t = (0:Sampling_Period:Sampling_Period*(Number_Of_Samples-1));
Signal_1 = Amplitude.*sin(2*pi*50*t);
subplot(3,1,1); plot(t,Signal_1,'Marker','.');
title("Signal 1: 50Hz Sinusoid Samples at " + num2str(Sampling_Frequency) + "Hz");
xlabel("Time (s)"); ylabel("Amplitude");
xlim([0 0.05]);

%Configuring Signal 2%
Sampling_Frequency = 4050;
Number_Of_Samples = 200;
Amplitude = 12*0.1;

Sampling_Period = 1/Sampling_Frequency;
t = (0:Sampling_Period:Sampling_Period*(Number_Of_Samples-1));
Signal_2 = Amplitude.*sin(2*pi*50*t);
subplot(3,1,2); plot(t,Signal_2,'Marker','.');
title("Signal 2: 50Hz Sinusoid Samples at " + num2str(Sampling_Frequency) + "Hz");
xlabel("Time (s)"); ylabel("Amplitude");
xlim([0 0.05]);

%Adding signals with respect to samples%
Cumulative_Signal = Signal_1 + Signal_2;
Samples_Vector = (0:199); Interpolated_Samples_Vector = linspace(0,199,250);
Cumulative_Signal = interp1(Samples_Vector,Cumulative_Signal,Interpolated_Samples_Vector);
subplot(3,1,3); plot(Cumulative_Signal,'Marker','.');
title("Summed with Respect to Samples: Cumulative Signal (Signal 1 + Signal 2)");
xlabel("Sample [n]"); ylabel("Amplitude");
xlim([0 250]);

Summation with Respect to Time:

Respect to Time

clf;
%Configuring Signal 1%
Sampling_Frequency = 8000;
Number_Of_Samples = 200;
Amplitude = 12*0.1;

Sampling_Period = 1/Sampling_Frequency;
t = (0:Sampling_Period:Sampling_Period*(Number_Of_Samples-1));
Signal_1 = Amplitude.*sin(2*pi*50*t);
subplot(3,1,1); plot(t,Signal_1,'Marker','.');
title("Signal 1: 50Hz Sinusoid Samples at " + num2str(Sampling_Frequency) + "Hz");
xlabel("Time (s)"); ylabel("Amplitude");
xlim([0 0.05]);

%Configuring Signal 2%
Sampling_Frequency = 4050;
Number_Of_Samples = 200;
Amplitude = 12*0.1;

Sampling_Period = 1/Sampling_Frequency;
t = (0:Sampling_Period:Sampling_Period*(Number_Of_Samples-1));
Signal_2 = Amplitude.*sin(2*pi*50*t);
subplot(3,1,2); plot(t,Signal_2,'Marker','.');
title("Signal 2: 50Hz Sinusoid Samples at " + num2str(Sampling_Frequency) + "Hz");
xlabel("Time (s)"); ylabel("Amplitude");
xlim([0 0.05]);

%Adding signals with respect to time%
Signal_1 = [Signal_1 zeros(1,200)];
Signal_1 = Signal_1(1:2:end);
Samples_Vector = (0:199); Interpolated_Samples_Vector = linspace(0,199,250);
Signal_1 = interp1(Samples_Vector,Signal_1,Interpolated_Samples_Vector);
Signal_2 = interp1(Samples_Vector,Signal_2,Interpolated_Samples_Vector);
End_Time = Sampling_Period*(Number_Of_Samples-1);
t = linspace(0,End_Time,250);
Cumulative_Signal = Signal_1 + Signal_2;
subplot(3,1,3); plot(t,Cumulative_Signal,'Marker','.');
title("Summed with Respect to Time: Cumulative Signal (Signal 1 + Signal 2)");
xlabel("Time (s)"); ylabel("Amplitude");
xlim([0 0.05]);

Summation with Respect to Time But Varying Amount of Start Samples:

Adjusted with Respect to Time

clf;
%Configuring Signal 1%
Sampling_Frequency = 8000;
Number_Of_Samples = 200;
Amplitude = 12*0.1;

Sampling_Period = 1/Sampling_Frequency;
t = (0:Sampling_Period:Sampling_Period*(Number_Of_Samples-1));
Signal_1 = Amplitude.*sin(2*pi*50*t);
subplot(3,1,1); plot(t,Signal_1,'Marker','.');
title("Signal 1: 50Hz Sinusoid Samples at " + num2str(Sampling_Frequency) + "Hz (" + num2str(Number_Of_Samples) + " samples)");
xlabel("Time (s)"); ylabel("Amplitude");
xlim([0 0.05]);

%Configuring Signal 2%
Sampling_Frequency = 4050;
Number_Of_Samples = 100;
Amplitude = 12*0.1;

Sampling_Period = 1/Sampling_Frequency;
t = (0:Sampling_Period:Sampling_Period*(Number_Of_Samples-1));
Signal_2 = Amplitude.*sin(2*pi*50*t);
subplot(3,1,2); plot(t,Signal_2,'Marker','.');
title("Signal 2: 50Hz Sinusoid Samples at " + num2str(Sampling_Frequency) + "Hz (" + num2str(Number_Of_Samples) + " samples)");
xlabel("Time (s)"); ylabel("Amplitude");
xlim([0 0.05]);

Signal_1 = Signal_1(1:2:end);
Cummulative_Signal = Signal_1 + Signal_2;
Samples_Vector = (0:99); Interpolated_Samples_Vector = linspace(0,99,250);
Cummulative_Signal = interp1(Samples_Vector,Cummulative_Signal,Interpolated_Samples_Vector);
End_Time = Sampling_Period*(Number_Of_Samples-1);
t = linspace(0,End_Time,250);
subplot(3,1,3); plot(t,Cummulative_Signal,'Marker','.');
title("Summed with Respect to Time");
xlabel("Time (s)"); ylabel("Amplitude");
xlim([0 0.05]);

Rna using MATLAB R2019b

Upvotes: 1

Kenneth Boyd
Kenneth Boyd

Reputation: 667

The part about comparing the length of signals in 1a and 1c does not make sense to me. They can be whatever length you want. In my sample code, I made each signal 1000 points long so that there would always be plenty of points available for plotting. If you want to get the length of a vector, you could use the length function.

To add the two signals, they should be the same sample rate, so in my sample code, I used interp1 to downsample the higher sample rate signal to the same sample rate as the lower signal.

To control how many points are plotted, I make a variable called k that is used to control which points are plotted.

Here is the code to do what is asked:

amp = 12*0.1; %amplitude

%create a
fs_a = 8000; %sampling frequency
t_a = (0:999) / fs_a;
a = amp*sin(2*pi*50*t_a);

%create c
fs_c = 4050; %sampling frequency
t_c = (0:999) / fs_c;
c = amp*sin(2*pi*50*t_c);

% plot a and c
k = 1:200;
figure(1)
plot(t_a(k), a(k), '+-', t_c(k), c(k), 'o-');
xlabel('Time (s)')
grid on
legend('1a', '1c')

% Add a and c
% resample a a the sample rate for c (4050 Hz)
a_resampled = interp1(t_a, a, t_c);
length_a_plus_c = min([length(c), length(a_resampled)]);
a_plus_c = a_resampled(1:length_a_plus_c) + c(1:length_a_plus_c);

% plot a + c
figure(2)
k = 1:250;
plot(t_c(k), a_plus_c(k), 'o-')
grid on
xlabel('Time (s)')
title('a + c')

Upvotes: 2

Related Questions