summaryrefslogtreecommitdiffstats
path: root/Master/Modellbildung_und_Simulation/Aufgabenblatt1/wave_fft.m
diff options
context:
space:
mode:
Diffstat (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt1/wave_fft.m')
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt1/wave_fft.m53
1 files changed, 53 insertions, 0 deletions
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt1/wave_fft.m b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/wave_fft.m
new file mode 100644
index 0000000..8d12feb
--- /dev/null
+++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt1/wave_fft.m
@@ -0,0 +1,53 @@
+wavefile = 'Testdatei 400 Hz 1.wav';
+M = wavread(wavefile); % load data from wave file
+x = M(:,1); % wave samples as vector
+fs = 16000; % sample frequency
+sec = 0.025; % time span
+x = x(1:(sec*fs)+1); % samples of time span
+wavwrite(x,16000,16,"x.wav")
+N = length(x); % number of samples
+t = [0:1/fs:sec]; % time vector
+
+nfft = 2^(nextpow2(N)); % Use next highest power of 2 greater than or equal to length(x) to calculate FFT.
+NumUniquePts = ceil((nfft+1)/2); % Calculate the numberof unique points
+fftx = fft(x,nfft); % Take fft, padding with zeros so that length(fftx) is equal to nfft
+save fftfile fftx; % Write fft complex data to file
+p = unwrap(angle(fftx)); % phase
+
+ifftx = ifft(fftx);
+ifftx = ifftx(1:N);
+wavwrite(ifftx,16000,16,"ifftx.wav")
+
+fftx = fftx(1:NumUniquePts); % FFT is symmetric, throw away second half
+magX = abs(fftx)/N; % Take the magnitude of fft of x and scale the fft so that it is not a function of the length of x
+magX = magX.^2; % Take the square of the magnitude of fft of x.
+
+% Since we dropped half the FFT, we multiply mx by 2 to keep the same energy.
+% The DC component and Nyquist component, if it exists, are unique and should not be multiplied by 2.
+if rem(nfft, 2) % odd nfft excludes Nyquist point
+ magX(2:end) = magX(2:end)*2;
+else
+ magX(2:end -1) = magX(2:end -1)*2;
+end
+f = (0:NumUniquePts-1)*fs/nfft; % This is an evenly spaced frequency vector with NumUniquePts points.
+
+figure(1);
+subplot(5,1,1);
+plot(t,x);
+title('time domain')
+xlabel('time');
+ylabel('Amplitude');
+
+subplot(5,1,3);
+plot(f,magX);
+xlabel('f/Hz');
+ylabel('Amplitudenspektrum');
+title('frequency domain');
+
+subplot(5,1,5);
+px = (1:1:length(p));
+plot(px,p);
+title('phase');
+
+% print -Ppng 'plot.png'
+