필터 지우기
필터 지우기

Matlab code for sine wave with varying frequency

조회 수: 11 (최근 30일)
Anisia Anil
Anisia Anil 2021년 9월 1일
댓글: Anisia Anil 2021년 9월 2일
How do I generate a sine wave with varying frequency and constant amplitude? I've tried the following code, but it is showing harmonics. How do i eliminate the harmoics?
clc;
fs = 2000; %sampling frequency
dt = 1/fs; %seconds per sample
StopTime = 10; %seconds
t = (0:dt:StopTime);
F = 50.*t; %Sinewave frequency {Hertz)
amp = 1000;
for i= 1:length(t);
data(i) = amp*sin(2*pi*F(i)*t(i));
end;
audiowrite('varFsin.wav', data, fs);
sound(data);
figure(1)
clf(1);
plot(t, data)
title('Sine Wave');
figure(2)
spectrogram(data, [80], [40], [256], fs, 'yaxis')
y = fft{data};
n = length(data); %number of samples
f = (0:n-1)*(fs/n); %frequency range
power = abs(y).^2/n; %power of the DFT
figure(3);
plot(f, power)
xlbel('frequency')
ylabel('power')

채택된 답변

Chunru
Chunru 2021년 9월 1일
clc;
fs = 2000; %sampling frequency
dt = 1/fs; %seconds per sample
StopTime = 10; %seconds
t = (0:dt:StopTime);
F = 50.*t; %Sinewave frequency {Hertz)
amp = 1000;
The following is equivalent to LFM (linear frequency modulation) signal.
for i= 1:length(t);
data(i) = amp*sin(2*pi*F(i)*t(i));
end;
%audiowrite('varFsin.wav', data, fs);
sound(data);
figure(1)
clf(1);
plot(t, data)
title('Sine Wave');
figure(2)
spectrogram(data, [80], [40], [256], fs, 'yaxis')
y = fft(data);
n = length(data); %number of samples
f = (0:n-1)*(fs/n); %frequency range
power = abs(y).^2/n; %power of the DFT
figure(3);
powershift = fftshift(power);
fshift = (-n/2:n/2-1)*(fs/n); % zero-centered frequency range
plot(fshift,powershift)
xlabel('frequency')
ylabel('power')
This is not really harmonic. The spectrum has transition region around 0 and +/-fs/2. You have no harmonics to remove.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by