Signal_Final_TikeaTE/windowing.m

112 lines
3.0 KiB
Matlab

function windowing(freq_signal, sampling_freq, phase_signal,win_duration, duration_signal, plt)
%{
fuction windowing(freq_signal, sampling_freq, phase_signal,win_duration, duration_signal, plt)
Ex: windowing(10, 40, 0, 2, 6, 1)
Task: to apply the time window of the signal x (any signal)
Inputs:
-win_sig: duration of the window
-freq_signal: frequency of the signal (in hz)
-sampling_freq: sampling frequency (how many sample per second) (in hz)
-phase_signal: signal phase (in rad)
-duration: how long u want the signal to be
-plt: if greater than 0, signal will be plotted
Output:
none
%}
[t,x] = sampling(freq_signal, sampling_freq, phase_signal, duration_signal, 0);
% create the rectangle window:
rect = createtemporalWindow(t, duration_signal, win_duration, sampling_freq, 1);
% apply the rectangle window
I_rect = applyTemporalwindow(x,t,rect);
% plot the rectangular window
if (plt)
figure;
plot(t, x, 'r'); hold on;
plot(t, I_rect, 'b');
xlabel('Time (s)');
ylabel('Signal amplitude (n.u.)');
legend('original', 'windowed');
end
% create the Hanning window:
Hanning = createtemporalWindow(t, duration_signal, win_duration, sampling_freq, 2);
% apply the hanning window
I_hanning = applyTemporalwindow(x,t,Hanning);
% plot the hanning window
if (plt)
plot(t,I_hanning,'g');
legend('original', 'rect','hanning');
end
% create the Hamming window:
Hamming = createtemporalWindow(t, duration_signal, win_duration, sampling_freq, 3);
% apply the hamming window
I_hamming = applyTemporalwindow(x,t,Hamming);
% plot the hamming window
if (plt)
plot(t,I_hamming,'k');
legend('original', 'rect','hanning','hamming');
end
% create the blackman window:
blackman = createtemporalWindow(t, duration_signal, win_duration, sampling_freq, 4);
% apply the blackman window
I_blackman = applyTemporalwindow(x,t,blackman);
% plot the rectangular window
if (plt)
plot(t,I_blackman,'c');
legend('original', 'rect','hanning','hamming','blackman');
end
% compute power for each window
[f_rect, P_rect] = frequencySpectrum(I_rect,sampling_freq,0);
[f_hanning, P_hanning] = frequencySpectrum(I_hanning,sampling_freq,0);
[f_hamming, P_hamming] = frequencySpectrum(I_hamming,sampling_freq,0);
[f_blackman, P_blackman] = frequencySpectrum(I_blackman,sampling_freq,0);
% plotting
if(plt)
figure;
% find max value and its index of DFT
[val, ind] = max(P_rect);
plot(f_rect,10*log10(P_rect/P_rect(ind)),'b');
hold on;
% find max value and its index of DFT
[val, ind] = max(P_hanning);
plot(f_hanning,10*log10(P_hanning/P_hanning(ind)),'r');
hold on;
% find max value and its index of DFT
[val, ind] = max(P_hamming);
plot(f_hamming,10*log10(P_hamming/P_hamming(ind)),'k');
hold on;
% find max value and its index of DFT
[val, ind] = max(P_blackman);
plot(f_blackman,10*log10(P_blackman/P_blackman(ind)),'g');
hold on;
xlabel('frequency (hz)');
ylabel('Power (dB)');
legend('rect','hanning','hamming','blackman');
xlim([0 sampling_freq]);
end