Matlab code for sine wave with varying frequency

44 次查看(过去 30 天)
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 中查找有关 Fourier Analysis and Filtering 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by