Plotting sinewave looks weird.

20 views (last 30 days)
Dexter James Barit
Dexter James Barit on 17 Oct 2020
Commented: Image Analyst on 9 Nov 2022
I'm trying to plot a sinewave with a length of 1000 samples, with a sample rate of 40kHz and center frequency at 19kHz. For some reason when I plot the sinewave it looks weird. Am I plotting this correctly?
fs=40000;
freq=19000;
w=freq/fs;
n=0:999;
x1 = sin(2*pi*w*n);
plot(x1)

Answers (1)

Image Analyst
Image Analyst on 18 Oct 2020
This is what I get
% Plot a sinewave with a length of 1000 samples, with a sample rate of 40kHz and center frequency at 19kHz.
dt = 1/40000 % Samples are separated by 1/40000 of a second.
% 19 kHz means the period is 1/19000 of a second.
period = 1/19000
% Use 1000 samples
t = dt * (1 : 1000);
y = sin(2 * pi * t / period);
plot(t, y, 'b-');
grid on;
xlabel('time', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
  4 Comments
Suhas
Suhas on 9 Nov 2022
if sampling frequency is twice the highest frequency in signal i.e if sampling frequency > 19*2 = 38kHz , shouldn't that be enough according to nyquist theorom?
Image Analyst
Image Analyst on 9 Nov 2022
If you sample more than twice per period you can still have the appearance of aliasing. Look below where I plot just over the first dozen period, and look at the blue spots where the samples are taken from. If you plotted just the blue dots with lines between them it would look unlike the original signal.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 30;
% Plot a sinewave with a length of 1000 samples, with a sample rate of 40kHz and center frequency at 19kHz.
dt = 1/40000 % Samples are separated by 1/40000 of a second.
% 19 kHz means the period is 1/19000 of a second.
period = 1/19000
% Plot 12 periods of the full, unsampled waveform.
t = linspace(0, 12 * period, 2000);
y = sin(2 * pi * t / period);
plot(t, y, 'r-', 'LineWidth', 2);
grid on;
% Use samples spaced at dt up through the 12th period.
tMax = max(t);
t = 0 : dt : tMax;
y = sin(2 * pi * t / period);
hold on;
plot(t, y, 'b.', 'MarkerSize', markerSize);
grid on;
xlabel('time', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
legend('Full Unsampled Waveform', 'Sample Locations')
fontsize(gca, 15, 'points');
See, in each period there is about 3 samples.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!