changing frequency and divide 2 section

7 views (last 30 days)
Deniz S
Deniz S on 29 Jan 2016
Answered: Geoff Hayes on 29 Jan 2016
hi everyone,
i have created a sinusoidal signal having frequency 100Hz in MATLAB. In below you see the code, i want to ask 2 thing.
  1. Firstly, i want to divide 2 sections. 1st half 100hz and 2nd half 500hz.
  2. Secondly, frequency starting 100hz and increasing to 1000hz. 100 to 1000.
fSampling = 10000; %Sampling Frequency
tSampling = 1/fSampling; %Sampling Time
L = 10000; %Length of Signal
t = (0:L-1)*tSampling; %Time Vector
F = 100; %Frequency of Signal
%%Signal Without Noise
xsig = sin(2*pi*F*t);
subplot(2,1,1)
plot(t,xsig);
grid on;
axis([0 0.1 -1.2 +1.2])
xlabel('\itTime Axis \rightarrow');
ylabel('\itAmplitude \rightarrow');
title('\itxsig(t) of Frequency = 100Hz');
pause(2);
%%Frequency Transform of above Signal
subplot(2,1,2)
NFFT = 2^nextpow2(L);
Xsig = fft(xsig,NFFT)/L;
f1 = fSampling/2*(linspace(0,1,NFFT/2+1));
semilogy(f1,2*abs(Xsig(1:NFFT/2+1)),'r');
grid on;
axis([-50 500 1.0000e-005 1])
title('\itSignle-Sided Amplitude Sepectrum of xsig(t)');
xlabel('\itFrequency(Hz) \rightarrow');
ylabel('|Xsig(f)| \rightarrow');
pause(2);
%%Addition of Noise in Signal when Signal is Transmitted over some
% transmission medium
xnoise = xsig +0.25*randn(size(t));
figure;
subplot(2,1,1)
plot(t,xnoise,'r');
grid on;
xlabel('\itTime Axis \rightarrow');
ylabel('\itAmplitude \rightarrow');
title('\itxnoise(t) of Frequency = 100Hz (Noise Addition)');
pause(2);
%%Frequency Transform of the Noisy Signal
subplot(2,1,2)
NFFT = 2^nextpow2(L);
XNoise = fft(xnoise,NFFT)/L;
f1 = fSampling/2*(linspace(0,1,NFFT/2+1));
semilogy(f1,2*abs(XNoise(1:NFFT/2+1)),'r');
grid on;
axis([-50 500 1.0000e-005 1])
title('\itSingle-Sided Amplitude Spectrum of xnoise(t)');
xlabel('\itFrequency(Hz) \rightarrow');
ylabel('|XNoise(f)| \rightarrow');
pause(2);
%%Comparision of both Spectrums
figure;
subplot(2,1,1)
semilogy(f1,2*abs(Xsig(1:NFFT/2+1)));
grid on;
axis([-50 500 1.0000e-005 1])
title('\itSingle-Sided Amplitude Spectrum of xsig(t)');
xlabel('\itFrequency(Hz) \rightarrow');
ylabel('|Xsig(f)| \rightarrow');
subplot(2,1,2)
semilogy(f1,2*abs(XNoise(1:NFFT/2+1)),'r');
grid on;
axis([-50 500 1.0000e-005 1])
title('\itSingle-Sided Amplitude Spectrum of xnoise(t)');
xlabel('\itFrequency(Hz) \rightarrow');
ylabel('|XNoise(f)| \rightarrow');

Answers (1)

Geoff Hayes
Geoff Hayes on 29 Jan 2016
Deniz - if I understand your question correctly, for the part one you want the first section (the first 5000 samples) to be generated from a sinusoidal signal at 100 Hz and the second section (the remaining 5000 samples) to be generated from a sinusoidal signal at 500 Hz. Look at how your are generating your signal
xsig = sin(2*pi*F*t);
where F is 100 Hz. This would be equivalent to
a = ones(1,L);
xsig = sin(2*pi*F*a.*t);
where we F*a is just an array with all elements equal to 100 Hz. Then, F*a.*t is just he pairwise multiplication of each element of F*a with t. This doesn't change what you have already - we are still generating a 100 Hz sinusoidal signal. But to get the second half at 500 Hz, we just set the second half of a to 5 so that F*a is an array of 100s (the first half) and 500s (the second half)
a = ones(1,L); % or a = ones(size(t));
a(L/2+1:end) = 5;
xsig = sin(2*pi*F*a.*t);
Now, for your second part, you want the frequency to run from 100 Hz to 1000 Hz. Does this mean you want a linear transition from 100 Hz to 1000 Hz, or every one hundred samples to be at a multiple of 100 Hz or something else? Do you expect to analyze this data using the same code from above or with something else?

Community Treasure Hunt

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

Start Hunting!