Search code examples
matlabsignal-processing

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?


Solution

  • 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')