Low-Pass High-Pass Bandpass Bandstop: Laborator 4
Low-Pass High-Pass Bandpass Bandstop: Laborator 4
Low-Pass High-Pass Bandpass Bandstop: Laborator 4
Master: GMRV
Laborator 4
Exercitiu 1:
Low-pass
High-pass
Bandpass
Bandstop
Exercitiu 2:
Ordinul 3
Ordinul 9
Ordinul 11
Ordinul 31
Exercitiu 3:
Violin
Ideal
band
pass
Ideal
band
stop
Ideal
high
pass
Low
pass
COD:
struct WAV_HEADER {
// Header RIFF
char chunkID[4];
unsigned long chunkSize;
char format[4];
// Subchunk fmt
char subChunk1ID[4];
unsigned long subChunk1Size;
unsigned short audioFormat;
unsigned short numChannels;
unsigned long sampleRate;
unsigned long byteRate;
unsigned short blockAlign;
unsigned short bitsPerSample;
// Subchunk data
char subChunk2ID[4];
unsigned long subChunk2Size;
};
// Open .wav
fopen_s(&file_ptr, path, "rb");
// Read header
if (file_ptr != 0)
fread(&header, sizeof(header), 1, file_ptr);
fclose(file_ptr);
return header;
}
// Number o samples
int sampleCount = (header.subChunk2Size * 8) / (header.numChannels *
header.bitsPerSample);
int bytesPerSample = header.bitsPerSample / 8;
// Close file
if (file_ptr != 0)
fclose(file_ptr);
return samples;
}
// Create header
WAV_HEADER wav_header = header;
fclose(file_ptr);
}
// Number of samples
float N = samples.size();
// Calculate DFT
for (int n = 0; n < N; ++n)
{
float angle = 2 * M_PI * k * n / N;
re += samples[n] * cos(angle);
im -= samples[n] * sin(angle);
}
// Calculate magnitude
magnitudeVector[k] = sqrt(re * re + im * im);
if (re == 0)
phaseVector[k] = atan(0);
else
phaseVector[k] = atan(im / re);
dftTransform.push_back(std::complex<float>(re, im));
}
return dftTransform;
}
float divN = 1 / N;
// Calcualte IDFT
for (int k = 0; k < N; ++k)
{
float angle = 2 * M_PI * k * n / N;
// Fill vector
samplesIDFT.push_back(re + im);
}
// Reverse IDFT
std::reverse(samplesIDFT.begin(), samplesIDFT.end());
return samplesIDFT;
}
return IDFT(filtered_dft);
}
std::vector<int> freqResponse(dft.size());
std::vector<std::complex<float>> filtered_dft;
return IDFT(filtered_dft);
}
std::vector<float> GenerateLowPassFIR(
// fc = cutoff frequency
// N = order of filter
int N,
float fc,
float samplingRate)
{
std::vector<float> lp_filter(N, 0);
// Hanning
for (int i = 0; i < N; i++)
lp_filter[i] *= 0.5 * (1 - cos((2 * M_PI * i) / N));
// Write in file
std::ofstream out_file(window_path);
std::ostream_iterator<float> output_iterator(out_file, "\n");
std::copy(lp_filter.begin(), lp_filter.end(), output_iterator);
return lp_filter;
}
return convolutionRes;
}