Issue with generating a sine wave via linspace and colon

조회 수: 6 (최근 30일)
Swapnil srivastava
Swapnil srivastava 2023년 9월 12일
댓글: DGM 2023년 9월 12일
Hi Everyone, I'm having an issue with generating a sine wave using the linspace operator vs colon operator. The code is below:
%% Generate a sinusoidal wave
fc = 300e6; % center frequency
duration = 5e-3; % Total duration
fs = 10e6; % Sampling Rate
N = fs * duration; % Total number of samples
% Method 1, using linspace
t1 = linspace(0,duration,N);
% Method 2, using colon
t = 0:1/fs:duration;
% Plotting both of them
x1=sin(2*pi*fc*t1); x = sin(2*pi*fc*t);
figure;
subplot(211)
plot(t1,x1)
subplot(212)
plot(t,x)
Running this Code gives a very different result for the colon operator as can be seen clearly. Obviously I can just use the linspace function, but the main issue I have is that I'm trying to generate the sine wave in C++, and in there there's no linspace equivalent, and I'm using getting the same output as shown in the 2nd image. Hence I'm trying to debug this issue in MATLAB first.
From what I can tell, the two time variables are mostly identical, but differ in maybe 5th-6th decimal place, which shouldn't be enough to cause such massive discrepency. If anyone can help that'd be appreciated.
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 9월 12일
편집: Dyuman Joshi 2023년 9월 12일
> "From what I can tell, the two time variables are mostly identical"
No.
linspace(x1,x2,N) generates n points with spacing (x2-x1)/(N-1), which in your case is duration/(N-1) and the spacing for colon is 1/fs, which is equal to duration/N (from the definition of N). Different spacing means the resulting vectors will not be same/similar.
To make them identical theoretically, use N+1 points with linspace
Also, Use sinpi(x) instead of sin(pi*x) as it is more accurate - sinpi

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2023년 9월 12일
편집: DGM 2023년 9월 12일
You're trying to draw a sine wave with a frequency 30x higher than your sampling rate. The output is nothing but aliased nonsense in both cases. If you make t1 have N-1 samples, you'll see that the illusory sine wave disappears. Neither output is good.
Make some adjustments:
% Generate a sinusoidal wave
fc = 300e6; % center frequency
duration = 50e-9; % Total duration (short enough to be viewable)
fs = 10e9; % Sampling Rate (high enough to produce meaningful output)
N = fs * duration; % Total number of samples
% Method 1, using linspace
t1 = linspace(0,duration,N);
% Method 2, using colon
t2 = 0:1/fs:duration;
% Plotting both of them
x1 = sin(2*pi*fc*t1);
x2 = sin(2*pi*fc*t2);
subplot(211)
plot(t1,x1)
subplot(212)
plot(t2,x2)
  댓글 수: 3
Bruno Luong
Bruno Luong 2023년 9월 12일
Make it
t1 = linspace(0,duration,N+1)
DGM
DGM 2023년 9월 12일
That too, yes. I wasn't really paying attention to the difference in vector length, but if you want equivalence ...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by