Plotting sinewave looks weird.

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)

Respuestas (1)

Image Analyst
Image Analyst el 18 de Oct. de 2020

0 votos

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 comentarios

Dexter James Barit
Dexter James Barit el 18 de Oct. de 2020
Thanks for the feedback. I'm just confused why doens't it look like a normal sinwave? Is this how it suppose to look like?
Image Analyst
Image Analyst el 18 de Oct. de 2020
Dexter, look at what my code shows:
dt =
2.5e-05
period =
5.26315789473684e-05
So you're taking just barely over 2 samples per period You're sampling every 25 microseconds but the period is 52.6 microseconds -- that's not enough samples to give you enough points to follow one up/down period nice and smoothly. That also means those two sample points will be at slightly different points in each period. The sample points at each period shift slightly as they move along. So the amplitude will vary because you're plucking out the values at different points along the sine wave. This is called aliasing and is why you see a lower beat frequency instead of the main frequency. You're undersampling it. In your class did the instructor mention anything about aliasing or the Nyquist Frequency? Or beat frequency? If not, study up on that.
Suhas
Suhas el 9 de Nov. de 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?
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.

Iniciar sesión para comentar.

Preguntada:

el 17 de Oct. de 2020

Comentada:

el 9 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by