Saad Muftah
Junior Member level 1
- Joined
- Oct 10, 2013
- Messages
- 15
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 3
- Activity points
- 107
function y = my_DFT(sig,N)
for k=1 : N-1
re_sum =0
im_sum =0
for n=1:N-1
cosf = cos((2*%pi*n*k)/N)
sinf = sin((2*%pi*n*k)/N)
re_sum = re_sum + real(sig(n))*cosf-imag(sig(n))*sinf
im_sum = im_sum + real(sig(n))*sinf+imag(sig(n))*cosf
end
y(k)= re_sum + (%i * im_sum)
end
endfunction
t=0:0.001:0.1
signal = sin(2*%pi*100*t)
plot(abs(my_DFT(signal,100)))
Hi everyone
I am trying to do matlab code to calculate the real and imaginary values of fundamental frequency sine wave by discrete fourier transform for one period of 16 sample.
close all; clear all; clc;
% ============= (1) Time-domain Signal ============= %
% Frequency of the sine in Hertz
f_sine = 377;
% Period of the sine
T_sine = 1/f_sine;
% Create "Nsamps" equally-spaced points in time from 0 to T_sine
Nsamps = 16;
t = (0:Nsamps-1)*T_sine/Nsamps;
% Period between each sample of t
T_samp = t(2)-t(1);
f_samp = 1/T_samp; % Sampling frequency
% Create the sine signal at time points t
x = sin(2*pi*f_sine*t);
% Plot time-domain signal
figure();
plot(t,x);
title('Time-domain signal, x(t)');
xlabel('Time');
ylabel('Amplitude');
grid on; axis tight;
% ========== (2) Frequency-domain Signal =========== %
% Frequency difference between each frequency bin in spectrum
df = f_samp/Nsamps;
% All frequencies to plot in spectrum
f = df*(0:Nsamps-1);
% Discrete Fourier Transform (using FFT method)
Y = fft(x);
% Plot frequency spectrum...
figure();
% Magnitude part
subplot(2,1,1);
plot(f,abs(Y));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
% Phase part
subplot(2,1,2);
plot(f,unwrap(angle(Y))*180/pi); % in degrees (unwrapped)
xlabel('Frequency (Hz)');
ylabel('Phase (degrees)');
grid on;
Thanks for the algorithm, but i have couple of questions:
1. why the magnitudes in the frequency domain are not the same as the signal it self, should it scaled.
Energy_x = sum(abs(x).^2)
Energy_Y = (1/Nsamps)*sum(abs(Y).^2)
2. and if you could help me in that, i want to build code extract magnitude of one frequency from signal that change with time, because the signal is transient and the harmonics change with time, so i want to see the behavior of one frequency with time.
Y = fftshift(Y);
f = [-(ceil((Nsamps-1)/2):-1:1)*df 0 (1:floor((Nsamps-1)/2))*df];
I am still not sure exactly what you mean. When you say "absorb", do you mean remove? If you could provide more detail and an example (preferably with the data you want to process), I may be able to help.i want to absorb the magnitude of second harmonic of transient signal that decay with time, so my question how to make code for extracting the second harmonic in fourier spectrum.
This is a large field, so I certainly wouldn't consider myself an expert. Personally, I just developed a bit of experience by working with examples in Matlab/C++ and searching the internet when I got stuck. However, I have a friend (who sat next to me during our PhDs) who is an expert and he highly recommends the following book for beginners:Another question if you don't mind how to get more knowledge in this field precisely, how to be more professional at this field, again many thanks,
fid = fopen('filename.dat');
x = fread(fid,'float');
[max_val, max_loc] = max(abs(Y));
f_peak = f(max_loc);
close all; clear all; clc;
% Total number of data samples in time domain
L = 1024*1024;
% Sliding window size in time domain (spectrum size in frequency domain)
Nfft = 512;
% Set the amount the windows overlap in the time domain
overlap = 0.5; % e.g. 50% overlap
% Generate some test data. Example: sine wave in Gaussian noise
x = sin(2*pi*(1/32)*(0:L-1)) + 10*randn(1,L);
% Plot a few sine periods (may look very noisy)
plot(x(1:128));
grid on;
axis tight;
title('Time-domain Input Data');
% Allocate storage for spectrum (i.e. Power Spectral Density estimate)
PSD = zeros(1,Nfft);
% Set window weights
w = gausswin(Nfft,5).'; % (e.g. a Gaussian window)
% Initialise sample index (i.e. where the window starts)
i = 1;
% Keep count of the number of FFTs that go into the PSD estimation
count_ffts = 0;
% Slide across all data
while i <= L - Nfft + 1
% Select the next segment of data and multiply with window weights
x_window = w.*x(i:i+Nfft-1);
% Calculate power spectrum (unnormalised)
P = abs(fft(x_window)).^2;
% Add this power estimate to our total
PSD = PSD + P;
count_ffts = count_ffts+1;
% Increment sample index for next loop iteration
i = i + round(Nfft*(1-overlap));
end
figure();
plot((0:Nfft-1),PSD);
title(['Unnormalised spectrum, averaged over ', num2str(count_ffts), 'spectra']);
grid on;
axis tight;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?