diff --git a/.gitignore b/.gitignore index c53779c493b900d72c648ff2e308e574e822f2c3..c1f963e837dfdc36f8b48565164de257ebe6c4b4 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,18 @@ Ex5/matlab/ex5_task1a.flt Ex5/matlab/ex5_task1.flt Ex5/ex5_task1.dot~ Ex5/matlab/ex5_task1.wav + +Ex6/out_win_dbg +Ex6/out_win_rls +Ex6/out_linux_dbg +Ex6/out_linux_rls +Ex6/ex6_task1_ch1.flt +Ex6/Ex6_task1_dbg.exe +Ex6/Ex6_task1.dot +Ex6/ex6_task1.flt +Ex6/ex6_task1.wav +Ex6/Ex6_task1b_dbg.exe +Ex6/Ex6_task2_dbg.exe +Ex6/Ex6_task2_test_dbg.exe +Ex6/log_file.txt +Ex6/SymbolMapper_test_dbg.exe diff --git a/Ex3/.vscode/launch.json b/Ex3/.vscode/launch.json index 3ebdbd93e6a13d674460f2ecf90a6eb881f9dfdf..38fb3bb2fa4c686f481c28d8422777c343cfe9cf 100644 --- a/Ex3/.vscode/launch.json +++ b/Ex3/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "(gdb) Ex3 â widnows debug run ", + "name": "(gdb) Ex3 â windows debug run ", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/Ex3_task3_dbg.exe", diff --git a/Ex4/.vscode/launch.json b/Ex4/.vscode/launch.json index 6d8497af78ab0f2b26d81fc538c9deb9b1c7c35d..9fbccc52c324cc584cd17c4fe9ffeef876aa7a35 100644 --- a/Ex4/.vscode/launch.json +++ b/Ex4/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "(gdb) Ex4 â widnows debug run ", + "name": "(gdb) Ex4 â windows debug run ", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/Ex4_task2_dbg.exe", diff --git a/Ex5/.vscode/launch.json b/Ex5/.vscode/launch.json index 244c24446a515be4dda1087a13c0206140e887b3..22536e70d12f3259dce340aac2af5ee7a0800aa4 100644 --- a/Ex5/.vscode/launch.json +++ b/Ex5/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "(gdb) Ex5 â widnows debug run ", + "name": "(gdb) Ex5 â windows debug run ", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/Ex5_task1_dbg.exe", diff --git a/Ex6/.vscode/c_cpp_properties.json b/Ex6/.vscode/c_cpp_properties.json new file mode 100644 index 0000000000000000000000000000000000000000..11c576f694c99f7b30d0d5015bc0fede255966c2 --- /dev/null +++ b/Ex6/.vscode/c_cpp_properties.json @@ -0,0 +1,65 @@ +{ + "configurations": [ + { + "name": "Windows-Release", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/../DSPE_lib_minGW/MinGW-W64_8.1.0/include/**" + ], + "defines": [ + "WIN32", + "__DEBUG__=0" + ], + //"compilerPath": "gcc.exe", + "cStandard": "c11", + "cppStandard": "c++11", + "intelliSenseMode": "gcc-x64" + }, + { + "name": "Linux-Release", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/../DSPE_lib_minGW/MinGW-W64_8.1.0/include/**" + ], + "defines": [ + "WIN32", + "__DEBUG__=0" + ], + "compilerPath": "gcc.exe", + "cStandard": "c11", + "cppStandard": "c++11", + "intelliSenseMode": "gcc-x64" + }, + { + "name": "Windows-Debug", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/../DSPE_lib_minGW/MinGW-W64_8.1.0/include/**" + ], + "defines": [ + "WIN32", + "__DEBUG__=1" + ], + //"compilerPath": "gcc.exe", + "cStandard": "c11", + "cppStandard": "c++11", + "intelliSenseMode": "gcc-x64" + }, + { + "name": "Linux-Debug", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/../DSPE_lib_minGW/MinGW-W64_8.1.0/include/**" + ], + "defines": [ + "WIN32", + "__DEBUG__=1" + ], + "compilerPath": "gcc.exe", + "cStandard": "c11", + "cppStandard": "c++11", + "intelliSenseMode": "gcc-x64" + } +], + "version": 4 +} \ No newline at end of file diff --git a/Ex6/.vscode/launch.json b/Ex6/.vscode/launch.json new file mode 100644 index 0000000000000000000000000000000000000000..8e2a1d7721907969007fb9a2ec844fd74e792b50 --- /dev/null +++ b/Ex6/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Ex6 â windows debug run ", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/Ex6_task1_dbg.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", +// "miDebuggerPath": "d:/CodeBlocks_20_03/MinGW/bin/gdb.exe", + "setupCommands": [ + { + "description": "WĹÄ cz formatowanie kodu dla gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "Build Ex6_task1.cpp" + } + ] +} \ No newline at end of file diff --git a/Ex6/.vscode/settings.json b/Ex6/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..7539f57a1c00d23c72572860bf1d9fdb330c6afd --- /dev/null +++ b/Ex6/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.associations": { + "Makefile": "makefile", + "ostream": "cpp", + "memory": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/Ex6/.vscode/tasks.json b/Ex6/.vscode/tasks.json new file mode 100644 index 0000000000000000000000000000000000000000..d6a8e82ac226c44f14ff182826f9d3d73dfa739d --- /dev/null +++ b/Ex6/.vscode/tasks.json @@ -0,0 +1,70 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + // https://code.visualstudio.com/docs/editor/tasks + // ${command:cpptools.activeConfigName} + "tasks": [ + { + "label": "Build Ex6_task1.cpp", + "type": "shell", + "command": "make build -f Makefile.main FILE=Ex6_task1 VS_CFG=${command:cpptools.activeConfigName}", + "group": "build", + "presentation": { + "echo": true + }, + "problemMatcher": "$gcc" + }, + { + "label": "Build Ex6_task1b.cpp", + "type": "shell", + "command": "make build -f Makefile.main FILE=Ex6_task1b VS_CFG=${command:cpptools.activeConfigName}", + "group": "build", + "presentation": { + "echo": true + }, + "problemMatcher": "$gcc" + }, + { + "label": "Build Ex6_task2.cpp", + "type": "shell", + "command": "make build -f Makefile.main FILE=Ex6_task2 VS_CFG=${command:cpptools.activeConfigName}", + "group": "build", + "presentation": { + "echo": true + }, + "problemMatcher": "$gcc" + }, + { + "label": "Build Ex6_task2_test.cpp", + "type": "shell", + "command": "make build -f Makefile.main FILE=Ex6_task2_test VS_CFG=${command:cpptools.activeConfigName}", + "group": "build", + "presentation": { + "echo": true + }, + "problemMatcher": "$gcc" + }, + { + "label": "Build SymbolMapper_test.cpp", + "type": "shell", + "command": "make build -f Makefile.main FILE=SymbolMapper_test VS_CFG=${command:cpptools.activeConfigName}", + "group": "build", + "presentation": { + "echo": true + }, + "problemMatcher": "$gcc" + }, + { + "label": "Clean Ex6", + "type": "shell", + "command": "make clean -f Makefile.main VS_CFG=${command:cpptools.activeConfigName}", + "group": "build", + "presentation": { + "echo": true + }, + "problemMatcher": "$gcc" + } + + ] +} \ No newline at end of file diff --git a/Ex6/Ex6_task1.cbp b/Ex6/Ex6_task1.cbp new file mode 100644 index 0000000000000000000000000000000000000000..fd1105bc32cd38c105d85834d3556b2783790ec5 --- /dev/null +++ b/Ex6/Ex6_task1.cbp @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> +<CodeBlocks_project_file> + <FileVersion major="1" minor="6" /> + <Project> + <Option title="Ex6_task1" /> + <Option pch_mode="2" /> + <Option compiler="gcc" /> + <Build> + <Target title="default"> + <Option output="Ex6_task1" prefix_auto="1" extension_auto="1" /> + <Option type="1" /> + <Option compiler="gcc" /> + </Target> + </Build> + <Compiler> + <Add option="-std=c++0x" /> + <Add option="-m32" /> + <Add option="-g" /> + <Add option="-DWIN32" /> + <Add directory="../DSPE_lib_minGW/MinGW-W64_8.1.0/include" /> + <Add directory="../DSPE_lib_minGW/MinGW-W64_8.1.0/include/dbg" /> + </Compiler> + <Linker> + <Add option="-static-libgcc" /> + <Add option="-m32" /> + <Add library="DSPE" /> + <Add library="winmm" /> + <Add directory="../DSPE_lib_minGW/MinGW-W64_8.1.0/dbg" /> + </Linker> + <Unit filename="Ex6_task1.cpp" /> + <Extensions> + <code_completion /> + <envvars /> + <debugger /> + <lib_finder disable_auto="1" /> + </Extensions> + </Project> +</CodeBlocks_project_file> diff --git a/Ex6/Ex6_task1.cpp b/Ex6/Ex6_task1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..719b10ebbdc3a4a4624d4d1fc38ee6b0c10f64b8 --- /dev/null +++ b/Ex6/Ex6_task1.cpp @@ -0,0 +1,254 @@ +/*! Laboratory: Advanced signal processing of digital telecommunications + * (Zaawansowane przetwarzanie sygnaĹĂłw telekomunikacji cyfrowej) + * Ex. 6. task 1. + * + * \author Marek Blok + * \date 2021.09.15 + */ +#include <DSP_lib.h> +#include <memory> + +int main(int argn, char *args[]) +{ + UNUSED_ARGUMENT(argn); + UNUSED_ARGUMENT(args); + + /*! Idea: + * - three channels are used - different modulations - the same shaping filter + * DPSK, pi/4-QPSK, (MSK), OQPSK, QAM-16, OOK + * - FFT - 32 ==> directly placing at intermediate frequency (oversampling 32) + * - available channels 1..15 (for FMT adjacent channels should not be used) + * using: 8, 10, 13 + * other channels are set to zero: SetConstInput + */ + /*************************************************************/ + // Log file setup + DSP::log.SetLogFileName("log_file.txt"); + DSP::log.SetLogState(DSP::e::LogState::file | DSP::e::LogState::console); + + DSP::log << DSP::lib_version_string() << std::endl << std::endl; + /*************************************************************/ + + long int Fp2, F_symb; + DSP::LoadCoef coef_info; + int N_rc; + unsigned int L; + DSP::Float_vector h_rc; + + coef_info.Open("ex6_task1_h_rc.coef", "matlab"); + N_rc = coef_info.GetSize(0); + if (N_rc < 1) + { + DSP::log << DSP::e::LogMode::Error << "No ex6_task1_h_rc.coef: aborting" << std::endl; + return -1; + } + else + { + coef_info.Load(h_rc); + F_symb = coef_info.Fp; + } + /*************************************************************/ + int bits_per_symbol = 2; + + DSP::Clock_ptr BitClock, SymbolClock, OutputClock; + SymbolClock = DSP::Clock::CreateMasterClock(); + BitClock = DSP::Clock::GetClock(SymbolClock, bits_per_symbol, 1); + + + //DSP::u::WaveInput AudioIn(MasterClock, "test.wav", "."); + //F_symb = AudioIn.GetSamplingRate(); + + + // szybkoĹÄ symbolowa + F_symb = 1500; + + // dĹugoĹÄ transformaty DFT / liczba podkanaĹĂłw + // jeĹźeli K jest caĹkowitÄ potÄgÄ dwĂłjki moĹźliwe jest uĹźycie FFT radix-2 +// int K = 16; + int K = 32; + L = K; + + // wyjĹciowa szybkosÄ prĂłbkowania + Fp2 = K*F_symb; + + + string tekst = "Fsymb = " + to_string(int(F_symb)) + ", Fp2 = " + to_string(int(Fp2)) + ", L = " + to_string(L); + DSP::log << tekst << std::endl; + + OutputClock=DSP::Clock::GetClock(SymbolClock, K, 1); + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // Pierwszy kanaĹ danych + DSP::u::FileInput BinData1(BitClock, "Ex6_task1.cpp", 1U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + BinData1.SetName("Ex6_task1.cpp (binary)"); + //DSP::u::PSKencoder PSKencoder1(DSP::e::PSK_type::QPSK_A); + DSP::u::Serial2Parallel SPconv1(BitClock, bits_per_symbol); + DSP::u::SymbolMapper PSKencoder1(DSP::e::ModulationType::PSK, bits_per_symbol, 0.0); // QPSK_A + PSKencoder1.SetName("QPSK_A"); + + // ====================================================================== // + DSP::u::FileOutput FileOutCh1("ex6_task1_ch1.flt", DSP::e::SampleType::ST_float, 2, DSP::e::FileType::FT_flt, F_symb); + FileOutCh1.SetName("ex6_task1_ch1.flt"); + + PSKencoder1.Output("out") >> FileOutCh1.Input("in"); + // ====================================================================== // + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // drugi kanaĹ danych + DSP::u::FileInput BinData2(BitClock, "Ex6_task2.cpp", 1U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + BinData2.SetName("Ex6_task2.cpp (binary)"); + //DSP::u::PSKencoder PSKencoder2(DSP::e::PSK_type::QPSK_B); + DSP::u::Serial2Parallel SPconv2(BitClock, bits_per_symbol); + DSP::u::SymbolMapper PSKencoder2(DSP::e::ModulationType::PSK, bits_per_symbol, DSP::M_PIf/4); // QPSK_B + PSKencoder2.SetName("QPSK_B"); + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // trzeci kanaĹ danych + DSP::u::FileInput BinData3(BitClock, "Ex6_task1b.cpp", 1U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + BinData3.SetName("Ex6_task1b.cpp (binary)"); + //DSP::u::PSKencoder PSKencoder3(DSP::e::PSK_type::QPSK_A); + DSP::u::Serial2Parallel SPconv3(BitClock, bits_per_symbol); + DSP::u::SymbolMapper PSKencoder3(DSP::e::ModulationType::PSK, bits_per_symbol, 0.0); // QPSK_A + PSKencoder3.SetName("QPSK_A"); + + //BinData1.Output("out"), PSKencoder1.Input("in")); + BinData1.Output("out") >> SPconv1.Input("in"); + SPconv1.Output("out") >> PSKencoder1.Input("in"); + + //BinData2.Output("out"), PSKencoder2.Input("in")); + BinData2.Output("out") >> SPconv2.Input("in"); + SPconv2.Output("out") >> PSKencoder2.Input("in"); + + //BinData3.Output("out"), PSKencoder3.Input("in")); + BinData3.Output("out") >> SPconv3.Input("in"); + SPconv3.Output("out") >> PSKencoder3.Input("in"); + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // FFT z filtrami polifazowymi (tylko dla uĹźywanych kanaĹĂłw) + + int channel1, channel2, channel3; + // numery aktywnych podkanaĹĂłw + // mumeracja podkanaĹĂłw: 0, 1, ..., K-1 + channel1 = 1; channel2 = 4; channel3 = 6; + + auto fft = make_shared<DSP::u::FFT>(K); + for (int ind = 0; ind < K; ind++) + { + if ((ind != channel1) && (ind != channel2) && (ind != channel3)) + { + // definiujemy wejĹcia o staĹej wartoĹci 0+j0 (wszystkie poza uĹźywanymi) + string text = "in" + to_string(ind+1); + fft->SetConstInput(text, 0.0, 0.0); + } + } + + // podĹÄ cz generatory wÄ skopasmowe ST + // kanaĹ nr 8 + tekst = "in" + to_string(channel1+1); + PSKencoder1.Output("out") >> fft->Input(tekst); + // kanaĹ nr 10 + tekst = "in" + to_string(channel2+1); + PSKencoder2.Output("out") >> fft->Input(tekst); + // kanaĹ nr 13 + tekst = "in" + to_string(channel3+1); + PSKencoder3.Output("out") >> fft->Input(tekst); + + // filtry polifazowe + vector<shared_ptr<DSP::u::FIR> > H_g(K, nullptr); + vector<shared_ptr<DSP::u::Zeroinserter> > ZeroIns(K, nullptr); + vector<shared_ptr<DSP::u::Delay> > Z1(K, nullptr); + vector<shared_ptr<DSP::u::Addition> > Add(K, nullptr); + for (int ind = 0; ind < K; ind++ ) + { + // Filtry polifazowe (tworzenie filtrĂłw zintegrowane z dekompozycjÄ polifazowÄ odpowiedzi impulsowej h_rc): + // Filtr o odpowiedzi impulsowej zĹoĹźonej z co K-tej prĂłbki + // odpowiedzi impulsowej h_rc zaczynajÄ c od prĂłbki (K-1)-ind. + H_g[ind] = make_shared<DSP::u::FIR>(true, h_rc, (K-1)-ind, K); + ZeroIns[ind] = make_shared<DSP::u::Zeroinserter>(true, SymbolClock, K); + + string text = "out" + to_string(ind+1); + fft->Output(text) >> H_g[ind]->Input("in"); + H_g[ind]->Output("out") >> ZeroIns[ind]->Input("in"); + } + + for (int ind = 0; ind <K-1; ind++) + { + Z1[ind] = make_shared<DSP::u::Delay>(1,2); // zespolony (!!! potem wariant rzeczywisty) + Z1[ind]->SetName("Z^-1",false); + Add[ind] = make_shared<DSP::u::Addition>(0,2); // zespolony (!!! potem wariant rzeczywisty) + } + + ZeroIns[0]->Output("out") >> Z1[0]->Input("in"); + for (int ind = 1; ind < K; ind++) + { + ZeroIns[ind]->Output("out") >> Add[ind-1]->Input("in1"); + Z1[ind-1]->Output("out") >> Add[ind-1]->Input("in2"); + + if (ind < K-1) + { + Add[ind-1]->Output("out") >> Z1[ind]->Input("in"); + } + } + + + // Output to the soundcard + DSP::u::AudioOutput SoundOut(Fp2, 1, 16); + // Output to the mono 16bit *.wav file + DSP::u::FileOutput FileOut2a("ex6_task1.wav", DSP::e::SampleType::ST_short, 2, DSP::e::FileType::FT_wav, Fp2); + FileOut2a.SetName("ex6_task1.wav"); + DSP::u::FileOutput FileOut2b("ex6_task1.flt", DSP::e::SampleType::ST_float, 2, DSP::e::FileType::FT_flt, Fp2); + FileOut2b.SetName("ex6_task1.flt"); + + Add[K-2]->Output("out.re") >> SoundOut.Input("in"); + Add[K-2]->Output("out") >> FileOut2a.Input("in"); + Add[K-2]->Output("out") >> FileOut2b.Input("in"); + + ///////////////////////////////// + // check if there are signals + // connected to all inputs + DSP::Component::CheckInputsOfAllComponents(); + + // *********************************** // + DSP::Clock::SchemeToDOTfile(SymbolClock, "Ex6_task1.dot"); + + // *********************************** // + int SamplesInSegment = 512; + __int64 NoOfSamplesProcessed = 0; + // 10 seconds + #define MAX_SAMPLES_TO_PROCESS 5*Fp2 + while(NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS) + { + + // ********************************************************** // + DSP::Clock::Execute(OutputClock, SamplesInSegment); + // ********************************************************** // + + /* + if (BinData1.GetBytesRead() > 0) + { + NoOfSamplesProcessed = 0; // Play the whole file + } + else // Play 200ms more + { + if (NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS - Fp2/5) + NoOfSamplesProcessed = MAX_SAMPLES_TO_PROCESS - Fp2/5; + } + */ + + NoOfSamplesProcessed += SamplesInSegment; + // ********************************************************** // + } + + /*************************************************************/ + H_g.clear(); + ZeroIns.clear(); + Z1.clear(); + Add.clear(); + + fft.reset(); + + /*************************************************************/ + DSP::Clock::FreeClocks(); + + return 0; +} diff --git a/Ex6/Ex6_task1b.cbp b/Ex6/Ex6_task1b.cbp new file mode 100644 index 0000000000000000000000000000000000000000..52c25ff7028997d6678b4fb36cd9afd07904c29a --- /dev/null +++ b/Ex6/Ex6_task1b.cbp @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> +<CodeBlocks_project_file> + <FileVersion major="1" minor="6" /> + <Project> + <Option title="ex6_task1b" /> + <Option pch_mode="2" /> + <Option compiler="gcc" /> + <Build> + <Target title="default"> + <Option output="ex6_task1b" prefix_auto="1" extension_auto="1" /> + <Option type="1" /> + <Option compiler="gcc" /> + </Target> + </Build> + <Compiler> + <Add option="-std=c++0x" /> + <Add option="-m32" /> + <Add option="-g" /> + <Add option="-DWIN32" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/include" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/dbg" /> + </Compiler> + <Linker> + <Add option="-static-libgcc" /> + <Add option="-m32" /> + <Add library="DSPE" /> + <Add library="winmm" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/dbg" /> + </Linker> + <Unit filename="Ex6_task1b.cpp" /> + <Extensions> + <code_completion /> + <envvars /> + <debugger /> + <lib_finder disable_auto="1" /> + </Extensions> + </Project> +</CodeBlocks_project_file> diff --git a/Ex6/Ex6_task1b.cpp b/Ex6/Ex6_task1b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..017400f348139f9390c52df5f9980c7c2e542d23 --- /dev/null +++ b/Ex6/Ex6_task1b.cpp @@ -0,0 +1,239 @@ +/*! Laboratory: Advanced signal processing of digital telecommunications + * (Zaawansowane przetwarzanie sygnaĹĂłw telekomunikacji cyfrowej) + * Ex. 6. task 1. variant b + * + * \author Marek Blok + * \date 2021.09.15 + */ +#include <DSP_lib.h> +#include <memory> + +DSP::Float_ptr read_buffer = NULL; +int buffer_size, read_buffer_pointer = -1; +int No_of_samples=0; + + +int main(int argn, char *args[]) +{ + UNUSED_ARGUMENT(argn); + UNUSED_ARGUMENT(args); + + /*! Idea: + * - three channels - different modulations - the same shaping filter + * DPSK, pi/4-QPSK, (MSK), OQPSK, QAM-16, OOK + * - FFT - 32 ==> direct generation on intermediate frequency (oversampling 32) + * - available channels 1..15 (for FMT adjacent channels should not be used) + * using: 8, 10, 13 + * other channels are set to zero: SetConstInput + */ + /*************************************************************/ + // Log file setup + DSP::log.SetLogFileName("log_file.txt"); + DSP::log.SetLogState(DSP::e::LogState::file | DSP::e::LogState::console); + + DSP::log << DSP::lib_version_string() << std::endl << std::endl; + /*************************************************************/ + + long int Fp2, F_symb; + DSP::LoadCoef coef_info; + int N_rc; + unsigned int L; + DSP::Float_vector h_rc; + + coef_info.Open("ex6_task1_h_rc.coef", "matlab"); + N_rc = coef_info.GetSize(0); + if (N_rc < 1) + { + DSP::log << DSP::e::LogMode::Error << "No ex6_task1_h_rc.coef: aboarding" << std::endl; + return -1; + } + else + { + coef_info.Load(h_rc); + F_symb = coef_info.Fp; + } + /*************************************************************/ + + DSP::Clock_ptr BitClock, SymbolClock, OutputClock; + SymbolClock = DSP::Clock::CreateMasterClock(); + + + //DSP::u::WaveInput AudioIn(MasterClock, "test.wav", "."); + //F_symb = AudioIn.GetSamplingRate(); + + // DFT length == numer of subchannels + // for K is and integer power of two the FFT radix-2 can be used + int K = 32; + //int K = 16; + L = K; + + // symbol rate + F_symb = 1500; // for L = 32 => Fp2 = 48000 + + + // output sample rate + Fp2 = K*F_symb; + + DSP::log << "Fsymb = " << int(F_symb) << ", Fp2 = " << int(Fp2) << ", L = " << L << std::endl; + + BitClock = DSP::Clock::GetClock(SymbolClock, 2, 1); + OutputClock = DSP::Clock::GetClock(SymbolClock, K, 1); + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // First data channel + DSP::u::FileInput BinData1(BitClock, "Ex6_task1.cpp", 1U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + BinData1.SetName("Ex6_task1.cpp"); + //DSP::u::PSKencoder PSKencoder1(DSP::e::PSK_type::QPSK_A); + + DSP::u::FileOutput BinData_out1("ex6_task1_bin_a_new.flt", DSP::e::SampleType::ST_float, 1U, DSP::e::FileType::FT_flt, F_symb); + BinData_out1.SetName("ex6_task1_bin_a_new.flt"); + BinData1.Output("out") >> BinData_out1.Input("in"); + + DSP::u::Serial2Parallel SPconv1(BitClock, 2); + DSP::u::SymbolMapper PSKencoder1(DSP::e::ModulationType::PSK, 2, 0.0); // QPSK_A + + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // second data channel + DSP::u::FileInput BinData2(BitClock, "Ex6_task2.cpp", 1U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + BinData2.SetName("Ex6_task2.cpp"); + DSP::u::Serial2Parallel SPconv2(BitClock, 2); + DSP::u::SymbolMapper PSKencoder2(DSP::e::ModulationType::PSK, 2, DSP::M_PIf/4); // QPSK_B + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // third data channel + DSP::u::FileInput BinData3(BitClock, "Ex6_task1b.cpp", 1U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + BinData3.SetName("Ex6_task1.cpp"); + DSP::u::Serial2Parallel SPconv3(BitClock, 2); + DSP::u::SymbolMapper PSKencoder3(DSP::e::ModulationType::PSK, 2, 0.0); // QPSK_A + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + BinData1.Output("out") >> SPconv1.Input("in"); + SPconv1.Output("out") >> PSKencoder1.Input("in"); + + BinData2.Output("out") >> SPconv2.Input("in"); + SPconv2.Output("out") >> PSKencoder2.Input("in"); + + BinData3.Output("out") >> SPconv3.Input("in"); + SPconv3.Output("out") >> PSKencoder3.Input("in"); + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // FFT with polyphase filters (only for used channels) + + int channel1, channel2, channel3; + // subchannels numbering convention 0, 1, ..., K-1 + channel1 = 1; channel2 = 4; channel3 = 6; + + auto fft = make_shared<DSP::u::FFT>(K); + for (int ind = 0; ind < K; ind++) + { + if ((ind != channel1) && (ind != channel2) && (ind != channel3)) + { + string text = "in" + to_string(ind+1); + fft->SetConstInput(text, 0.0, 0.0); + } + } + + // connect narrowband single tone generators (ST) + // channel no 8 + std::string tekst; + + tekst = "in" + to_string(channel1+1); + PSKencoder1.Output("out") >> fft->Input(tekst); + DSP::u::FileOutput SymbData1("ex6_task1a_new.flt", DSP::e::SampleType::ST_float, 2U, DSP::e::FileType::FT_flt, F_symb); + SymbData1.SetName("ex6_task1a_new.flt"); + PSKencoder1.Output("out") >> SymbData1.Input("in"); + + + // channel no 10 + tekst = "in" + to_string(channel2+1); + PSKencoder2.Output("out") >> fft->Input(tekst); + // channel no 13 + tekst = "in" + to_string(channel3+1); + PSKencoder3.Output("out") >> fft->Input(tekst); + + // polyphase filters + vector<shared_ptr<DSP::u::FIR> > H_g(K, nullptr); + + + // *************************************************** // + DSP::u::Parallel2Serial OutputBuffer(SymbolClock, K, 2, true); + + for (int ind = 0; ind < K; ind++) + { + // Polyphase filters (joint filters' creation and polyphase decomposition of impulse response h_rc): + // Filter with impulse response composed of every K-th sample + // of impulse response h_rc starting from (K-1)-th sample. + H_g[ind] = make_shared<DSP::u::FIR>(true, h_rc, (K-1)-ind, K); + + tekst = "out" + to_string(ind+1); + fft->Output(tekst) >> H_g[ind]->Input("in"); + +// tekst = "in" + to_string(2*ind+1); +// H_g[ind]->Output("out.re") >> OutputBuffer.Input(tekst); +// tekst = "in" + to_string(2*ind+2); +// H_g[ind]->Output("out.im") >> OutputBuffer.Input(tekst); + tekst = "in" + to_string(ind+1); + H_g[ind]->Output("out") >> OutputBuffer.Input(tekst); + } + + + // Output to the soundcard + DSP::u::AudioOutput SoundOut(Fp2, 1, 16); + // Output to the mono 16bit *.wav file + DSP::u::FileOutput FileOut2a("ex6_task1b.wav", DSP::e::SampleType::ST_short, 2, DSP::e::FileType::FT_wav, Fp2); + FileOut2a.SetName("ex6_task1b.wav"); + DSP::u::FileOutput FileOut2b("ex6_task1b.flt", DSP::e::SampleType::ST_float, 2, DSP::e::FileType::FT_flt, Fp2); + FileOut2b.SetName("ex6_task1b.flt"); + + OutputBuffer.Output("out1") >> SoundOut.Input("in"); + OutputBuffer.Output("out") >> FileOut2a.Input("in"); + OutputBuffer.Output("out") >> FileOut2b.Input("in"); + + ///////////////////////////////// + // check if there are signals + // connected to all inputs + DSP::Component::CheckInputsOfAllComponents(); + + // *********************************** // + DSP::Clock::SchemeToDOTfile(SymbolClock, "ex6_task1b.dot"); + + // *********************************** // + int SamplesInSegment = 512; + __int64 NoOfSamplesProcessed = 0; + // 10 seconds + #define MAX_SAMPLES_TO_PROCESS 5*Fp2 + while(NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS) + { + + // ********************************************************** // + DSP::Clock::Execute(OutputClock, SamplesInSegment); + // ********************************************************** // + + /* + if (BinData1.GetBytesRead() > 0) + { + NoOfSamplesProcessed = 0; // Play the whole file + } + else // Play 200ms more + { + if (NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS - Fp2/5) + NoOfSamplesProcessed = MAX_SAMPLES_TO_PROCESS - Fp2/5; + } + */ + + NoOfSamplesProcessed += SamplesInSegment; + // ********************************************************** // + + //sprintf(tekst, "NoOfSamplesProcessed = %i", int(NoOfSamplesProcessed)); + //DSPf_InfoMessage(tekst); + } + + /*************************************************************/ + H_g.clear(); + fft.reset(); + + /*************************************************************/ + DSP::Clock::FreeClocks(); + + return 0; +} diff --git a/Ex6/Ex6_task2.cbp b/Ex6/Ex6_task2.cbp new file mode 100644 index 0000000000000000000000000000000000000000..9916a6def221bf3d68479713673ab2b804d8707e --- /dev/null +++ b/Ex6/Ex6_task2.cbp @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> +<CodeBlocks_project_file> + <FileVersion major="1" minor="6" /> + <Project> + <Option title="ex6_task2" /> + <Option pch_mode="2" /> + <Option compiler="gcc" /> + <Build> + <Target title="default"> + <Option output="ex6_task2" prefix_auto="1" extension_auto="1" /> + <Option type="1" /> + <Option compiler="gcc" /> + </Target> + </Build> + <Compiler> + <Add option="-std=c++0x" /> + <Add option="-m32" /> + <Add option="-g" /> + <Add option="-DWIN32" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/include" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/dbg" /> + </Compiler> + <Linker> + <Add option="-static-libgcc" /> + <Add option="-m32" /> + <Add library="DSPE" /> + <Add library="winmm" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/dbg" /> + </Linker> + <Unit filename="Ex6_task2.cpp" /> + <Extensions> + <code_completion /> + <envvars /> + <debugger /> + <lib_finder disable_auto="1" /> + </Extensions> + </Project> +</CodeBlocks_project_file> diff --git a/Ex6/Ex6_task2.cpp b/Ex6/Ex6_task2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27ef822ec716987d1950f582819d675b1ea418b4 --- /dev/null +++ b/Ex6/Ex6_task2.cpp @@ -0,0 +1,270 @@ +/*! Laboratorium: Zaawansowane przetwarzanie sygna��w telekomunikacji cyfrowej + * ďż˝w. 6. zad. 2. + * + * \author Marek Blok + * \date 2018.03.16 + */ + +#include <sstream> +using namespace std; + +#include <DSP_lib.h> +#include <memory> + +DSP::Float_ptr read_buffer = NULL; +int buffer_size; +int No_of_samples=0; +// //int K = 16; int M = 4; // Oversampling ratio +//int K = 32; int M = 8; // Oversampling ratio : K/M +int K = 32; int M = 1; // Oversampling ratio : K/M + + +int main(int argn, char *args[]) +{ + /*! Koncepcja + * - cztery kanaďż˝y - rďż˝ne modulacje - ten sam filtr ksztaďż˝tujďż˝cy + * DPSK, pi/4-QPSK, (MSK), OQPSK, QAM-16, OOK + * - FFT - 16 ==> od razu lokowanie na PCz (nadporďż˝bkowanie 16) + * - dostďż˝pne kanaďż˝y 1..7 (nie moďż˝na uďż˝yďż˝ dla FMT dwďż˝ch sďż˝siednich ze wzglďż˝du na ICI) + * uďż˝ycie: 2, 5, 7 + * reszta staďż˝e zero !!! SetConstInput (chyba powinno dziaďż˝aďż˝) + */ + /*************************************************************/ + // Log file setup + DSP::log.SetLogFileName("log_file.txt"); + DSP::log.SetLogState(DSP::e::LogState::file | DSP::e::LogState::console); + + DSP::log << DSP::lib_version_string() << std::endl << std::endl; + /*************************************************************/ + + long int Fp2, F_symb; + DSP::LoadCoef coef_info; + int N_rc; + //unsigned int L; + DSP::Float_vector h_rc; + string fft_output_name; + string tekst, tekst2; + + map<string,shared_ptr<DSP::Component> > blocks; + + coef_info.Open("ex6_task1_h_rc.coef", "matlab"); + N_rc = coef_info.GetSize(0); + if (N_rc < 1) + { + DSP::log << DSP::e::LogMode::Error << "No ex6_task1_h_rc.coef: aboarding" << std::endl; + return -1; + } + else + { + coef_info.Load(h_rc); + for (auto n=0U; n<h_rc.size(); n++){ + h_rc[n] *= K; + } + F_symb = coef_info.Fp; + } + /*************************************************************/ + + DSP::Clock_ptr SymbolClock, InputClock; + InputClock=DSP::Clock::CreateMasterClock(); + + + //DSP::u::WaveInput AudioIn(MasterClock, "test.wav", "."); + //F_symb = AudioIn.GetSamplingRate(); + + + F_symb = 1500; // dla L = 32 => Fp2 = 48000 + Fp2 = K*F_symb; + + DSP::log << "Fsymb = " << int(F_symb) << ", Fp2 = " << int(Fp2) << ", K = " << K << ", M = " << M << std::endl; + + SymbolClock=DSP::Clock::GetClock(InputClock, 1, K/M); + + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // Multichannel input signal (for complex valued signal (2U)) + string input_filename; + + char mode = 'A'; + if (argn == 2) { + mode = args[1][0]; + } + unsigned int input_delay = 1U; + // 1 symbol delay at on the transmitting side - because of FFT + // 2 bit delay after decoder + // previous: SetSkip for 10 dibits + // new: 10 dibits + 1 dibit (additional symbol from Serial2Parallel converter located before DSP::u::SymbolMapper) + unsigned int bin_data_skip = (2*(1+ 2+8)); + + switch (mode) { + case 'A': + input_filename = "ex6_task1.flt"; + break; + case 'B': + input_filename = "ex6_task1b.flt"; + // skip additional symbol (compensate for the DSP::u::Parallel2Serial in the modulator) + input_delay += K; + bin_data_skip += 2*1; // skip additional dibit + break; + default: + DSP::log << DSP::e::LogMode::Error << "Unsupported mode" << std::endl; + return -1; + break; + } + blocks["InputSignal"] = make_shared<DSP::u::FileInput>(InputClock, input_filename, 2U, DSP::e::SampleType::ST_float, DSP::e::FileType::FT_flt); + blocks["InputSignal"]->SetName(input_filename); + blocks["DumpImag"] = make_shared<DSP::u::Vacuum>(); + + // "Frame"/ FFT symbol synchronization + blocks["SymbolTimigDelay"] = shared_ptr<DSP::Block>(new DSP::u::Delay (input_delay)); + + blocks["InputSignal"]->Output("out.re") >> blocks["SymbolTimigDelay"]->Input("in"); + blocks["InputSignal"]->Output("out.im") >> blocks["DumpImag"]->Input("in"); + + blocks["OutputBuffer"] = make_shared<DSP::u::Serial2Parallel>(InputClock, K,1); + blocks["SymbolTimigDelay"]->Output("out") >> blocks["OutputBuffer"]->Input("in"); + + blocks["fft"] = make_shared<DSP::u::FFT>(K, false); + // poliphase filters + //DSP::u::FIR *H_g[K]; + vector<shared_ptr<DSP::u::FIR> > H_g(K, nullptr); + + for (int ind = 0; ind < K; ind++) + { + H_g[ind] = make_shared<DSP::u::FIR>(h_rc, (K-1)-ind, K, M); + + tekst = "out" + to_string(ind+1); + tekst2 = "in" + to_string(ind+1); + blocks["OutputBuffer"]->Output(tekst) >> H_g[ind]->Input("in"); + H_g[ind]->Output("out") >> blocks["fft"]->Input(tekst2); + } + + // number of active subchannels + const int NoOfActiveChannels = 3; + + vector<shared_ptr<DSP::u::Vacuum> > Discard(K-NoOfActiveChannels, nullptr); + vector<shared_ptr<DSP::u::RawDecimator> > RawDec(NoOfActiveChannels, nullptr); + vector<shared_ptr<DSP::u::SymbolDemapper> > PSKdecoder(NoOfActiveChannels, nullptr); + vector<shared_ptr<DSP::u::Parallel2Serial> > PSconv(NoOfActiveChannels, nullptr); + vector<shared_ptr<DSP::u::FileOutput> > SymbData(NoOfActiveChannels, nullptr); + vector<shared_ptr<DSP::u::FileOutput> > BinData(NoOfActiveChannels, nullptr); + + // channel1 = 1; channel2 = 4; channel3 = 6; + // subchannels numbering convension: 0, 1, ..., K-1 + int channel_no[NoOfActiveChannels] = {1, 4, 6}; + + int current_discard_block_no = 0; + int active_channel_index = -1; + for (int ind = 0; ind < K; ind++) + { + active_channel_index = -1; + for (int ind2 = 0; ind2 < NoOfActiveChannels; ind2++) + { + if (ind == channel_no[ind2]) + { // memorise indexes of consecative active channels (indexing from zero) + active_channel_index = ind2; + } + } + + fft_output_name = "out" + to_string(ind+1); + if (active_channel_index == -1) + { // subchannel is not on the list of active channels + Discard[current_discard_block_no] = make_shared<DSP::u::Vacuum>(true); + //Discard[current_discard_block_no] = shared_ptr<DSP::u::Vacuum>(new DSP::u::Vacuum(true)); + blocks["fft"]->Output(fft_output_name) >> Discard[current_discard_block_no++]->Input("in"); + } + else + { // subchannel is on the list of active channels + stringstream filename; + +// PSKdecoder[active_channel_index] = make_shared<DSP::u::SymbolDemapper>(SymbolClock, DSP::e::ModulationType::PSK, 2, 0.0); //M_PIx1f/2); // QPSK_A + PSKdecoder[active_channel_index] = make_shared<DSP::u::SymbolDemapper>(DSP::e::ModulationType::PSK, 2, 0.0); // QPSK_A + PSconv[active_channel_index] = make_shared<DSP::u::Parallel2Serial>(SymbolClock, 2); + filename << "ex6_task2_symb_" << mode << "_" << char('a' + active_channel_index) << ".flt"; + SymbData[active_channel_index] = make_shared<DSP::u::FileOutput>(filename.str(), DSP::e::SampleType::ST_float, 2U, DSP::e::FileType::FT_flt, F_symb); + SymbData[active_channel_index]->SetName(filename.str()); + + filename.str(""); filename.clear(); + filename << "ex6_task2_bin_" << mode << "_" << char('a' + active_channel_index) << ".txt"; + BinData[active_channel_index] = make_shared<DSP::u::FileOutput>(filename.str(), DSP::e::SampleType::ST_bit, 1U, DSP::e::FileType::FT_raw); + //BinData[active_channel_index] = make_shared<DSP::u::FileOutput>(filename.str(), DSP::e::SampleType::ST_float, 1U, DSP::e::FileType::FT_raw); + BinData[active_channel_index]->SetName(filename.str()); + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // dibit offset - byte start synchronization + BinData[active_channel_index]->SetSkip(bin_data_skip); + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + + // FFT with poliphase filters (only for used channels) + if (M > 1) + { + RawDec[active_channel_index] = make_shared<DSP::u::RawDecimator>(SymbolClock, M, 2U); + blocks["fft"]->Output(fft_output_name) >> RawDec[active_channel_index]->Input("in"); + RawDec[active_channel_index]->Output("out") >> SymbData[active_channel_index]->Input("in"); + RawDec[active_channel_index]->Output("out") >> PSKdecoder[active_channel_index]->Input("in"); + } + else + { + RawDec[active_channel_index] = NULL; + blocks["fft"]->Output(fft_output_name) >> SymbData[active_channel_index]->Input("in"); + blocks["fft"]->Output(fft_output_name) >> PSKdecoder[active_channel_index]->Input("in"); + } + PSKdecoder[active_channel_index]->Output("out") >> PSconv[active_channel_index]->Input("in"); + PSconv[active_channel_index]->Output("out") >> BinData[active_channel_index]->Input("in"); + } + } + + + + // *********************************** // + DSP::Clock::SchemeToDOTfile(SymbolClock, "ex6_task2.dot"); + ///////////////////////////////// + // check if there are signals + // connected to all inputs + DSP::Component::CheckInputsOfAllComponents(); + + // *********************************** // + int SamplesInSegment = 512; + __int64 NoOfSamplesProcessed = 0; + // 10 seconds + #define MAX_SAMPLES_TO_PROCESS 50000 //1*Fp2 + while(NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS) + { + + // ********************************************************** // + DSP::Clock::Execute(InputClock, SamplesInSegment); + // ********************************************************** // + + /* + if (BinData1.GetBytesRead() > 0) + { + NoOfSamplesProcessed = 0; // Play the whole file + } + else // Play 200ms more + { + if (NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS - Fp2/5) + NoOfSamplesProcessed = MAX_SAMPLES_TO_PROCESS - Fp2/5; + } + */ + + NoOfSamplesProcessed += SamplesInSegment; + // ********************************************************** // + + //sprintf(tekst, "NoOfSamplesProcessed = %i", int(NoOfSamplesProcessed)); + //DSPf_InfoMessage(tekst); + } + + /*************************************************************/ + H_g.clear(); + blocks.clear(); + Discard.clear(); + PSKdecoder.clear(); + RawDec.clear(); + SymbData.clear(); + BinData.clear(); + + DSP::Clock::ListComponents(); + /*************************************************************/ + DSP::Clock::FreeClocks(); + + return 0; +} diff --git a/Ex6/Ex6_task2_test.cbp b/Ex6/Ex6_task2_test.cbp new file mode 100644 index 0000000000000000000000000000000000000000..126396bbf9b99e4ec0ad484a49b54f7de46c9f86 --- /dev/null +++ b/Ex6/Ex6_task2_test.cbp @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> +<CodeBlocks_project_file> + <FileVersion major="1" minor="6" /> + <Project> + <Option title="ex6_task2_test" /> + <Option pch_mode="2" /> + <Option compiler="gcc" /> + <Build> + <Target title="default"> + <Option output="ex6_task2_test" prefix_auto="1" extension_auto="1" /> + <Option type="1" /> + <Option compiler="gcc" /> + </Target> + </Build> + <Compiler> + <Add option="-std=c++0x" /> + <Add option="-m32" /> + <Add option="-g" /> + <Add option="-DWIN32" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/include" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/dbg" /> + </Compiler> + <Linker> + <Add option="-static-libgcc" /> + <Add option="-m32" /> + <Add library="DSPE" /> + <Add library="winmm" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/dbg" /> + </Linker> + <Unit filename="Ex6_task2_test.cpp" /> + <Extensions> + <code_completion /> + <envvars /> + <debugger /> + <lib_finder disable_auto="1" /> + </Extensions> + </Project> +</CodeBlocks_project_file> diff --git a/Ex6/Ex6_task2_test.cpp b/Ex6/Ex6_task2_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9966a29f8791e5550affdf2da29b381540cde501 --- /dev/null +++ b/Ex6/Ex6_task2_test.cpp @@ -0,0 +1,74 @@ +#include <DSP_lib.h> + +int main(void) +{ + DSP::Clock_ptr SymbolClock; + SymbolClock=DSP::Clock::CreateMasterClock(); + + // Tested: DSP_BPSK, DSP_DBPSK, DSP_DEBPSK + DSP::u::FileInput BinData1(SymbolClock, "Ex6_task1.cpp", 1U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + DSP::u::PSKencoder PSKencoder1(DSP::e::PSK_type::DEBPSK); + DSP::u::PSKdecoder PSKdecoder1(DSP::e::PSK_type::DEBPSK); + DSP::u::Amplifier PhaseRot(DSP::Complex(0.1,-0.8), 1, true); + DSP::u::FileOutput BinData_out("ex6_task2_text1.dat", DSP::e::SampleType::ST_bit, 1U, DSP::e::FileType::FT_raw); + + DSP::u::FileOutput Data_in("ex6_task2_text_in.flt", DSP::e::SampleType::ST_float, 1U, DSP::e::FileType::FT_flt); +BinData1.Output("out") >> Data_in.Input("in"); + DSP::u::FileOutput Data_en("ex6_task2_text_en.flt", DSP::e::SampleType::ST_float, 2U, DSP::e::FileType::FT_flt); +PhaseRot.Output("out") >> Data_en.Input("in"); + DSP::u::FileOutput Data_de("ex6_task2_text_de.flt", DSP::e::SampleType::ST_float, 1U, DSP::e::FileType::FT_flt); +PSKdecoder1.Output("out") >> Data_de.Input("in"); + +BinData1.Output("out") >> PSKencoder1.Input("in"); +PSKencoder1.Output("out") >> PhaseRot.Input("in"); +PhaseRot.Output("out") >> PSKdecoder1.Input("in"); +PSKdecoder1.Output("out") >> BinData_out.Input("in"); + + + // Tested: DSP::e::PSK_type::QPSK_A, DSP::e::PSK_type::QPSK_B + DSP::u::FileInput BinData2(SymbolClock, "Ex6_task1.cpp", 2U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + DSP::u::PSKencoder PSKencoder2(DSP::e::PSK_type::QPSK_A); + DSP::u::PSKdecoder PSKdecoder2(DSP::e::PSK_type::QPSK_A); + DSP::u::FileOutput BinData_out2("ex6_task2_text2.dat", DSP::e::SampleType::ST_bit, 2U, DSP::e::FileType::FT_raw); + +BinData2.Output("out") >> PSKencoder2.Input("in"); +PSKencoder2.Output("out") >> PSKdecoder2.Input("in"); +PSKdecoder2.Output("out") >> BinData_out2.Input("in"); + +// *********************************** // + DSP::Clock::SchemeToDOTfile(SymbolClock, "ex6_task2_test.dot"); + // *********************************** // + int SamplesInSegment = 512; + __int64 NoOfSamplesProcessed = 0; + // 10 seconds + #define MAX_SAMPLES_TO_PROCESS 1*1000 + while(NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS) + { + + // ********************************************************** // + DSP::Clock::Execute(SymbolClock, SamplesInSegment); + // ********************************************************** // + + /* + if (BinData1.GetBytesRead() > 0) + { + NoOfSamplesProcessed = 0; // Play the whole file + } + else // Play 200ms more + { + if (NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS - Fp2/5) + NoOfSamplesProcessed = MAX_SAMPLES_TO_PROCESS - Fp2/5; + } + */ + + NoOfSamplesProcessed += SamplesInSegment; + // ********************************************************** // + + //sprintf(tekst, "NoOfSamplesProcessed = %i", int(NoOfSamplesProcessed)); + //DSPf_InfoMessage(tekst); + } + + DSP::Clock::FreeClocks(); + + return 0; +} diff --git a/Ex6/Makefile b/Ex6/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..c94c79f51791f3dffcb970c87be83384cef43774 --- /dev/null +++ b/Ex6/Makefile @@ -0,0 +1,85 @@ +# Run: make Release +# Run: make Debug +CC=g++ +# comflag = -m32 +# comflag = -m64 +comflag = $(COMFLAG) + +DSPElib_DIR = ../../_DSPE_lib_minGW_/$(DSPElib_SUBDIR) + +SRC_DIR = . +SRC_CPP_SUBDIR = . + +#DFLAGS = -DWIN32 -DDEVCPP + +# -D INCLUDE_DSPE_EXAMPLES # TODO: uĹźycie w ramach kompilacji Main.cpp w trybie DEBUG +ifeq ($(MODE),Release) + CFLAGS = $(comflag) -std=c++0x -O3 -Wall -c -fmessage-length=0 -fno-strict-aliasing + LINKER_FLAGS = $(comflag) -s -static-libgcc -static-libstdc++ $(MISC_LINKER_FLAGS) + INCLUDES := -I"$(DSPElib_DIR)/include" -I"$(DSPElib_DIR)/include/rls" + DSPElib_FULLDIR = $(DSPElib_DIR)/rls + EXE_FILENAME = $(CPP_FILENAME)_rls.exe +else + CFLAGS = $(comflag) -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -W -Wshadow -Wconversion -fstrict-aliasing -fmax-errors=5 + LINKER_FLAGS = $(comflag) -static-libgcc -static-libstdc++ $(MISC_LINKER_FLAGS) + INCLUDES := -I"$(DSPElib_DIR)/include" -I"$(DSPElib_DIR)/include/dbg" + DSPElib_FULLDIR = $(DSPElib_DIR)/dbg + EXE_FILENAME = $(CPP_FILENAME)_dbg.exe +endif +# -U__STRICT_ANSI__ jest potrzebne do kompilacji debug_new.cpp, jezeli pominac ten plik to mozna rowniez wyrzucic te opcje +#CFLAGS_debug = $(comflag) -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -W -Wshadow -Wco#nversion -fstrict-aliasing -U__STRICT_ANSI__ + +SOURCES_NAMES = +SOURCES_NAMES += $(CPP_FILENAME).cpp +SOURCES = $(addprefix $(SRC_CPP_SUBDIR)/,$(SOURCES_NAMES)) + +SOURCES_DBG = +# SOURCES_DBG += $(SRC_DIR)/Main.cpp + +# ################################################# # +# DEBUG +OBJECTS := $(SOURCES:%.cpp=$(OUT_DIR)/%.o) +DEPENDS := $(SOURCES:%.cpp=$(OUT_DIR)/%.d) + +# ################################################# # +-include $(DEPENDS) + +all: build + + +# ########################################################################################### # +# ########################################################################################### # +build: $(SRC_DIR)/$(EXE_FILENAME) + +$(SRC_DIR)/$(EXE_FILENAME): $(OBJECTS) + @echo $(EXE_FILENAME) + $(CC) -L$(DSPElib_FULLDIR) $(OBJECTS) -o"$(SRC_DIR)/$(EXE_FILENAME)" $(LINKER_FLAGS) -lDSPE $(LIBS) + +# ########################################################################################### # +# ########################################################################################### # +# Z podanej listy usuwany $(OUT_DIR_WIN_RLS)/ oraz '.o' zamieniamy na '.cpp' +$(OBJECTS): $(OUT_DIR)/%.o : %.cpp + @echo $(@D) $< $@ + + #mkdir -p $(OUT_DIR)/$(SRC_CPP_SUBDIR) + mkdir -p $(@D) + $(CC) $(DFLAGS) $(CFLAGS) $(INCLUDES) -MMD $< -o $@ + + +clean: + @echo MODE: $(MODE) + + @if [ -d "$(OUT_DIR)" ]; then \ + echo "cleaning $(OUT_DIR_DBG) ..."; \ + #find $(OUT_DIR)/ -name "*.o" -type f -delete; \ + rm -rf $(OUT_DIR)/*.d; \ + rm -rf $(OUT_DIR)/*.o; \ + rm -rf $(OUT_DIR); \ + echo "cleaned $(OUT_DIR)"; \ + fi + rm -rf "$(SRC_DIR)/$(EXE_FILENAME)"; \ + #rm -rf "$(SRC_DIR)/*.gif"; \ + #rm -rf "$(SRC_DIR)/*.dot"; \ + +.PHONY: all build clean + diff --git a/Ex6/Makefile.main b/Ex6/Makefile.main new file mode 100644 index 0000000000000000000000000000000000000000..cf6c368f484646f2b2a80c30863e62c3d1e7ec7f --- /dev/null +++ b/Ex6/Makefile.main @@ -0,0 +1,60 @@ +# (View > Command Palette) => "Convert Indentation to Tabs" + +ifeq ($(VS_CFG),Windows-Debug) + MAKEFILE = "Makefile" + MODE = Debug + COMFLAG = -m64 + + OUT_DIR = ./out_win_dbg + DSPElib_SUBDIR = MinGW-W64_8.1.0 + MISC_LINKER_FLAGS = -static + LIBS = -lwinmm -lws2_32 + DFLAGS = -DWIN32 -DDEVCPP +endif +ifeq ($(VS_CFG),Windows-Release) + MAKEFILE = "Makefile" + MODE = Release + COMFLAG = -m64 + + OUT_DIR = ./out_win_rls + DSPElib_SUBDIR = MinGW-W64_8.1.0 + MISC_LINKER_FLAGS = -static + LIBS = -lwinmm -lws2_32 + DFLAGS = -DWIN32 -DDEVCPP +endif +ifeq ($(VS_CFG),Linux-Debug) + MAKEFILE = "Makefile" + MODE = Debug + COMFLAG = + + OUT_DIR = ./out_linux_dbg + DSPElib_SUBDIR = $(shell gcc -dumpmachine)-gcc_$(shell gcc -dumpversion) + MISC_LINKER_FLAGS = + LIBS := -lasound + DFLAGS = +endif +ifeq ($(VS_CFG),Linux-Release) + MAKEFILE = "Makefile" + MODE = Release + COMFLAG = + + OUT_DIR = ./out_linux_rls + DSPElib_SUBDIR = $(shell gcc -dumpmachine)-gcc_$(shell gcc -dumpversion) + MISC_LINKER_FLAGS = + LIBS := -lasound + DFLAGS = +endif + + + +build: + @echo "Building $(VS_CFG)" + @echo $(VS_CFG): $(MODE) // $(MAKEFILE) + make build CPP_FILENAME=$(FILE) MODE=$(MODE) COMFLAG=$(COMFLAG) DFLAGS="$(DFLAGS)" LIBS="$(LIBS)" OUT_DIR=$(OUT_DIR) DSPElib_SUBDIR=$(DSPElib_SUBDIR) MISC_LINKER_FLAGS="$(MISC_LINKER_FLAGS)" -f $(MAKEFILE) + +clean: + @echo "Cleaning $(VS_CFG)" + @echo $(VS_CFG): $(MODE) // $(MAKEFILE) + make clean MODE=$(MODE) OUT_DIR=$(OUT_DIR) -f $(MAKEFILE) + + diff --git a/Ex6/SymbolMapper_test.cbp b/Ex6/SymbolMapper_test.cbp new file mode 100644 index 0000000000000000000000000000000000000000..c8a3c05f2786c16e53139548962006e97df6607e --- /dev/null +++ b/Ex6/SymbolMapper_test.cbp @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> +<CodeBlocks_project_file> + <FileVersion major="1" minor="6" /> + <Project> + <Option title="SymbolMapper_test" /> + <Option pch_mode="2" /> + <Option compiler="gcc" /> + <Build> + <Target title="default"> + <Option output="SymbolMapper_test" prefix_auto="1" extension_auto="1" /> + <Option type="1" /> + <Option compiler="gcc" /> + </Target> + </Build> + <Compiler> + <Add option="-std=c++0x" /> + <Add option="-m32" /> + <Add option="-g" /> + <Add option="-DWIN32" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/include" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/dbg" /> + </Compiler> + <Linker> + <Add option="-static-libgcc" /> + <Add option="-m32" /> + <Add library="DSPE" /> + <Add library="winmm" /> + <Add directory="../DSPElib/CodeBlocks-m32_TDM_5.1.0/dbg" /> + </Linker> + <Unit filename="SymbolMapper_test.cpp" /> + <Extensions> + <code_completion /> + <envvars /> + <debugger /> + <lib_finder disable_auto="1" /> + </Extensions> + </Project> +</CodeBlocks_project_file> diff --git a/Ex6/SymbolMapper_test.cpp b/Ex6/SymbolMapper_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65d107d36cffd5ed9c7cb55ddb7b0877de68d62a --- /dev/null +++ b/Ex6/SymbolMapper_test.cpp @@ -0,0 +1,229 @@ +#include <sstream> +using namespace std; + +#include <DSP_lib.h> +#include <memory> + + +void test_symbol_sampler() +{ + map<string,shared_ptr<DSP::Component> > blocks; + + DSP::Clock_ptr BitClock, SymbolClock; + SymbolClock=DSP::Clock::CreateMasterClock(); + long F_symb = 1500; + int bits_per_symbol = 2; + BitClock = DSP::Clock::GetClock(SymbolClock, bits_per_symbol, 1); + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + DSP::log << "test_symbol_sampler()" << std::endl; + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // First data channel + blocks["BinData1"] = make_shared<DSP::u::FileInput>(BitClock, "ex6_task1.cpp", 1U, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + blocks["BinData1"]->SetName("ex6_task1.cpp"); //DSP::u::Const BinData1(BitClock, DSP::Float(-1.0)); + blocks["BinData_in"] = make_shared<DSP::u::FileOutput>("bits_in.txt", DSP::e::SampleType::ST_bit_text, 1U, DSP::e::FileType::FT_raw); + blocks["BinData_in"]->SetName("bits_in.txt"); + blocks["BinData1"]->Output("out") >> blocks["BinData_in"]->Input("in"); + + //DSP::u::PSKencoder PSKencoder1(DSP::e::PSK_type::QPSK_A); + blocks["SPconv"] = make_shared<DSP::u::Serial2Parallel>(BitClock, bits_per_symbol); + blocks["BinData1"]->Output("out") >> blocks["SPconv"]->Input("in"); + blocks["PSKencoder"] = make_shared<DSP::u::SymbolMapper>(DSP::e::ModulationType::PSK, bits_per_symbol, 0.0); // QPSK_A + blocks["SPconv"]->Output("out") >> blocks["PSKencoder"]->Input("in"); + + blocks["SymbData"] = make_shared<DSP::u::FileOutput>("symbols.flt", DSP::e::SampleType::ST_float, 2U, DSP::e::FileType::FT_flt, F_symb); + blocks["SymbData"]->SetName("symbols.flt"); + blocks["PSKencoder"]->Output("out") >> blocks["SymbData"]->Input("in"); + + blocks["PSKdecoder"] = make_shared<DSP::u::SymbolDemapper>(DSP::e::ModulationType::PSK, bits_per_symbol, 0.0); // QPSK_A + blocks["PSKencoder"]->Output("out") >> blocks["PSKdecoder"]->Input("in"); + blocks["PSconv"] = make_shared<DSP::u::Parallel2Serial>(SymbolClock, bits_per_symbol); + blocks["PSKdecoder"]->Output("out") >> blocks["PSconv"]->Input("in"); + + // SymbolMapper first sends "empty symbol" which is equivalent to bits_per_symbol zero bits + blocks["delay"] = shared_ptr<DSP::u::Delay>(new DSP::u::Delay(8-bits_per_symbol)); // byte boundary alignment + blocks["PSconv"]->Output("out") >> blocks["delay"]->Input("in"); + + blocks["BinData_raw"] = make_shared<DSP::u::FileOutput>("decoded_raw.txt", DSP::e::SampleType::ST_bit, bits_per_symbol, DSP::e::FileType::FT_raw); + blocks["BinData_raw"]->SetName("decoded_raw.txt"); + blocks["PSKdecoder"]->Output("out") >> blocks["BinData_raw"]->Input("in"); + + blocks["BinData_decoded"] = make_shared<DSP::u::FileOutput>("decoded.txt", DSP::e::SampleType::ST_bit, 1U, DSP::e::FileType::FT_raw); + blocks["BinData_decoded"]->SetName("decoded.txt"); + blocks["delay"]->Output("out") >> blocks["BinData_decoded"]->Input("in"); + (dynamic_cast<DSP::u::FileOutput *>(blocks["BinData_decoded"].get()))->SetSkip(8); // skip first 8 additional zero bits + + blocks["BinData_out"] = make_shared<DSP::u::FileOutput>("bits_out.txt", DSP::e::SampleType::ST_bit_text, 1U, DSP::e::FileType::FT_raw); + blocks["BinData_out"]->SetName("bits_out.txt"); + blocks["PSconv"]->Output("out") >> blocks["BinData_out"]->Input("in"); + (dynamic_cast<DSP::u::FileOutput *>(blocks["BinData_out"].get()))->SetSkip(0); + + blocks["BinData3"] = make_shared<DSP::u::FileOutput>("bits3.flt", DSP::e::SampleType::ST_float, 1U, DSP::e::FileType::FT_raw); + blocks["BinData3"]->SetName("bits3.flt"); + blocks["PSconv"]->Output("out") >> blocks["BinData3"]->Input("in"); + (dynamic_cast<DSP::u::FileOutput *>(blocks["BinData3"].get()))->SetSkip(0); + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + + + + // *********************************** // + DSP::Clock::SchemeToDOTfile(SymbolClock, "SymbolMapper-test.dot"); + ///////////////////////////////// + // check if there are signals + // connected to all inputs + DSP::Component::CheckInputsOfAllComponents(); + + // *********************************** // + int SamplesInSegment = 512; + __int64 NoOfSamplesProcessed = 0; + // 10 seconds + #define MAX_SAMPLES_TO_PROCESS 50000 //1*Fp2 + while(NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS) + { + + // ********************************************************** // + DSP::Clock::Execute(SymbolClock, SamplesInSegment); + // ********************************************************** // + +// if (BinData1.GetBytesRead() > 0) +// { +// NoOfSamplesProcessed = 0; // Play the whole file +// } +// else // Play 200ms more +// { +// if (NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS - Fp2/5) +// NoOfSamplesProcessed = MAX_SAMPLES_TO_PROCESS - Fp2/5; +// } + + NoOfSamplesProcessed += SamplesInSegment; + // ********************************************************** // + + //sprintf(tekst, "NoOfSamplesProcessed = %i", int(NoOfSamplesProcessed)); + //DSPf_InfoMessage(tekst); + } + + // ************************************************************* + blocks.clear(); + + DSP::Clock::ListComponents(); + // ************************************************************ + DSP::Clock::FreeClocks(); + +} + +void test_parallel_2_serial() +{ + map<string,shared_ptr<DSP::Component> > blocks; + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + DSP::log << "test_parallel_2_serial()" << std::endl; + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + + DSP::Clock_ptr BitClock, SymbolClock; + SymbolClock=DSP::Clock::CreateMasterClock(); + + unsigned int no_of_symbol_components = 2; //complex signals + int bits_per_symbol = 3; + BitClock = DSP::Clock::GetClock(SymbolClock, bits_per_symbol, 1); + + // First data channel + blocks["BinData1"] = make_shared<DSP::u::FileInput>(BitClock, "ex6_task1.cpp", no_of_symbol_components, DSP::e::SampleType::ST_bit, DSP::e::FileType::FT_raw); + blocks["BinData1"]->SetName("ex6_task1.cpp"); + //DSP::u::Const BinData1(BitClock, DSP::Float(-1.0)); + blocks["BinData1_txt"] = make_shared<DSP::u::FileOutput>("bits_in.txt", DSP::e::SampleType::ST_bit_text, no_of_symbol_components, DSP::e::FileType::FT_raw); + blocks["BinData1"]->Output("out") >> blocks["BinData1_txt"]->Input("in"); + + vector<DSP::Float> SP_init; + SP_init.resize(bits_per_symbol*no_of_symbol_components, 1.0); + blocks["SPconv"] = make_shared<DSP::u::Serial2Parallel>(BitClock, bits_per_symbol, no_of_symbol_components, SP_init); + blocks["BinData1"]->Output("out") >> blocks["SPconv"]->Input("in"); + blocks["BinData_SP_txt"] = make_shared<DSP::u::FileOutput>("bits_SP_out.txt", DSP::e::SampleType::ST_bit_text, bits_per_symbol*no_of_symbol_components, DSP::e::FileType::FT_raw); + blocks["SPconv"]->Output("out") >> blocks["BinData_SP_txt"]->Input("in"); + +// blocks["SymbData"] = make_shared<DSP::u::FileOutput>("symbols.flt", DSP::e::SampleType::ST_float, 2U, DSP::e::FileType::FT_flt); +// PSKencoder1.Output("out") >> blocks["SymbData"]->Input("in"); +// +// blocks["PSKdecoder"] = make_shared<DSP::u::SymbolDemapper>(SymbolClock, DSP::e::ModulationType::PSK, bits_per_symbol, 0.0); // QPSK_A +// PSKencoder1.Output("out") >> blocks["PSKdecoder"]->Input("in"); + + blocks["PSconv"] = make_shared<DSP::u::Parallel2Serial>(SymbolClock, bits_per_symbol, no_of_symbol_components); + blocks["SPconv"]->Output("out") >> blocks["PSconv"]->Input("in"); + + blocks["BinData2"] = make_shared<DSP::u::FileOutput>("bits.txt", DSP::e::SampleType::ST_bit, no_of_symbol_components, DSP::e::FileType::FT_raw); + blocks["PSconv"]->Output("out") >> blocks["BinData2"]->Input("in"); + (dynamic_cast<DSP::u::FileOutput *>(blocks["BinData2"].get()))->SetSkip(bits_per_symbol); // skip first bits_per_symbol additional zero symbols + + blocks["BinData2_txt"] = make_shared<DSP::u::FileOutput>("bits_out.txt", DSP::e::SampleType::ST_bit_text, no_of_symbol_components, DSP::e::FileType::FT_raw); + blocks["PSconv"]->Output("out") >> blocks["BinData2_txt"]->Input("in"); + blocks["BinData2b_txt"] = make_shared<DSP::u::FileOutput>("bits_out_b.txt", DSP::e::SampleType::ST_bit_text, no_of_symbol_components, DSP::e::FileType::FT_raw); + blocks["PSconv"]->Output("out") >> blocks["BinData2b_txt"]->Input("in"); + (dynamic_cast<DSP::u::FileOutput *>(blocks["BinData2b_txt"].get()))->SetSkip(bits_per_symbol); // skip first bits_per_symbol additional zero symbols + + // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + DSP::Clock::SchemeToDOTfile(SymbolClock, "Seria2Parallel-test.dot"); + ///////////////////////////////// + // check if there are signals + // connected to all inputs + DSP::Component::CheckInputsOfAllComponents(); + + // *********************************** // + int SamplesInSegment = 512; + __int64 NoOfSamplesProcessed = 0; + // 10 seconds + #define MAX_SAMPLES_TO_PROCESS 50000 //1*Fp2 + while(NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS) + { + + // ********************************************************** // + DSP::Clock::Execute(SymbolClock, SamplesInSegment); + // ********************************************************** // + + /* + if (BinData1.GetBytesRead() > 0) + { + NoOfSamplesProcessed = 0; // Play the whole file + } + else // Play 200ms more + { + if (NoOfSamplesProcessed < MAX_SAMPLES_TO_PROCESS - Fp2/5) + NoOfSamplesProcessed = MAX_SAMPLES_TO_PROCESS - Fp2/5; + } + */ + + NoOfSamplesProcessed += SamplesInSegment; + // ********************************************************** // + + //sprintf(tekst, "NoOfSamplesProcessed = %i", int(NoOfSamplesProcessed)); + //DSPf_InfoMessage(tekst); + } + + /*************************************************************/ + blocks.clear(); + + DSP::Clock::ListComponents(); + /*************************************************************/ + DSP::Clock::FreeClocks(); + +} + +int main(int argn, char *args[]) +{ + UNUSED_ARGUMENT(argn); + UNUSED_ARGUMENT(args); + + /*************************************************************/ + // Log file setup + DSP::log.SetLogFileName("log_file.txt"); + DSP::log.SetLogState(DSP::e::LogState::file | DSP::e::LogState::console); + + DSP::log << DSP::lib_version_string() << std::endl << std::endl; + /*************************************************************/ + + test_symbol_sampler(); + + //test_parallel_2_serial(); + return 0; +} diff --git a/Ex6/docs/Notes.docx b/Ex6/docs/Notes.docx new file mode 100644 index 0000000000000000000000000000000000000000..a455bf707fca15eeb61c3b3a897358eebb0a1087 Binary files /dev/null and b/Ex6/docs/Notes.docx differ diff --git a/Ex6/matlab - test/eyediagram2.m b/Ex6/matlab - test/eyediagram2.m new file mode 100644 index 0000000000000000000000000000000000000000..0ced67e5d8f51e6ebaeacb1199538a5f66c0042c --- /dev/null +++ b/Ex6/matlab - test/eyediagram2.m @@ -0,0 +1,69 @@ +function hf = eyediagram2(x, offset, Nsymb, hf) + +if nargin == 4, + figure(hf); +else + hf = figure; +end +subplot(1,1,1) +if nargin == 2, + Nsymb = offset; + offset = ceil((Nsymb-1)/2); +% Nsymb = length(x)+1; +end +x(ceil(length(x)/Nsymb)*Nsymb) = 0; +hp = []; +% t_ = linspace(-Nsymb/2, Nsymb/2, Nsymb); +t_ = 0:Nsymb; +ind_ = 1:(Nsymb+1); +% if rem(Nsymb,2) == 1, +% t_ = (0:Nsymb+1) - Nsymb/2 - 0.5; +% +% ind_ = [0:Nsymb+1]; +% else +% t_ = (-1:Nsymb) - Nsymb/2 + 0.5; +% +% ind_ = [-1:Nsymb]; +% end + + +dt = offset - round(offset); +t_ = t_ + dt; offset = round(offset); + +% t_ = t_ / Nsymb; + +if sum(abs(imag(x))) == 0, + subplot(1,1,1); + plot(t_, t_*0, 'k:'); + hold on + plot([0, 0], [-1.1, 1.1], 'k:'); + % plot(t_, +1+t_*0, 'k:'); + % plot(t_, -1+t_*0, 'k:'); + for ind = offset:Nsymb:length(x)-(Nsymb+1), + x_ = x(ind+ind_); + + hp(end+1) = plot(t_, real(x_)); +% hp(end+1) = plot(t_, imag(x_), 'r'); + end + hold off +else + subplot(1,2,1); + plot(t_, t_*0, 'k:'); + hold on + plot([0, 0], [-1.1, 1.1], 'k:'); + % plot(t_, +1+t_*0, 'k:'); + % plot(t_, -1+t_*0, 'k:'); + subplot(1,2,2); + plot(t_, t_*0, 'k:'); + hold on + plot([0, 0], [-1.1, 1.1], 'k:'); + for ind = offset:Nsymb:length(x)-(Nsymb+1), + x_ = x(ind+ind_); + + subplot(1,2,1); + hp(end+1) = plot(t_, real(x_)); + subplot(1,2,2); + hp(end+1) = plot(t_, imag(x_), 'r'); + end + hold off +end diff --git a/Ex6/matlab - test/load_filter_coef.m b/Ex6/matlab - test/load_filter_coef.m new file mode 100644 index 0000000000000000000000000000000000000000..8c8574c248dfce0cab89673b49921682f9957180 --- /dev/null +++ b/Ex6/matlab - test/load_filter_coef.m @@ -0,0 +1,107 @@ +function [coefficients, file_version] = load_filter_coef(filename) +% [coefficients, file_version] = load_filter_coef(filename) +% +% FIR filter coefficients +% coefficients.h +% - can be cell vector to store multiple FIR impulse responces +% For file_version >= 1 (default == 0), +% coefficients.Fp - sampling frequency +% +% This file is a part of Digital Signal Processing Engine +% \author Marek Blok +% \date 2008.03.28 + +% /*! File format (*.coef) - this is open format, for general use +% * (not only for storing coefficients) +% * +ind = find(filename == '.'); +if length(ind) == 0, + filename = [filename, '.coef']; +end +plik = fopen(filename, 'rb'); + +% * - (uchar) 1B - file version number +file_version = fread(plik, 1, 'uchar') + +switch file_version, + case 0, + case 1, + % * - (uint) 4B - Sampling frequency + coefficients.Fp = fread(plik, 1, 'uint32') + otherwise, + fclose(plik); + error('This version of coefficients file is unsupported'); +end + +% * - data - coefficients data (depends on fle version) +% * . +% * Data segment format: +% * -# (version: 0x00) +% * - (uchar) 1B - number of sample dimensions +% * 1 - real, 2 - complex, ... +no_of_channels = fread(plik, 1, 'uchar'); % complex +if no_of_channels == 1, + isComplex = 0; +elseif no_of_channels == 2, + isComplex = 1; +else + fclose(plik); + error('to many channels'); +end +% * - (uchar) 1B - sample component type +% * - DSP::e::FileType::FT_float (=1) : C++ float (32bit floating point) +% * - DSP::e::FileType::FT_short (=2) : C++ short (16bit signed integer) +% * - DSP::e::FileType::FT_uchar (=3) : C++ unsigned char (8bit unsigned integer with bias (0x80)) +% * - DSP::e::FileType::FT_double (=7) : C++ double (64bit floating point) +% * - DSP::e::FileType::FT_long_double (=8) : C++ long double (80bit floating point) +sample_type = fread(plik, 1, 'uchar'); +switch sample_type, + case 1, + sample_type = 'float'; + case 2, + sample_type = 'short'; + case 3, + sample_type = 'uchar'; + case 7, + sample_type = 'double'; + case 8, + sample_type = 'float80'; + otherwise + fclose(plik); + error('unknown sample type'); +end + +% * - (uchar) 1B - number of vectors +% * - 1 - FIR filter coefficients (one vector) +% * - 2 - IIR filter coefficients (two vectors) +% * - (x number of vectors) +% * - (ushort) 2B - number of samples in vector +% * - (x number of samples) +% * - (x number of sample dimensions) +% * - (sample componet type) xB - sample component +% * e.g. real, imag part +resp_no = fread(plik, 1, 'uchar'); + +if resp_no == 1, + N_FIR = fread(plik, 1, 'uint16'); + if isComplex, + dane = fread(plik, 2*N_FIR, sample_type); + coefficients.h = dane(1:2:2*N_FIR) + j*dane(2:2:2*N_FIR); + else + dane = fread(plik, N_FIR, sample_type); + coefficients.h = dane; + end +else + for ind_resp = 1:resp_no, + N_FIR = fread(plik, 1, 'uint16'); + if isComplex, + dane = fread(plik, 2*N_FIR, sample_type); + coefficients.h{ind_resp} = dane(1:2:2*N_FIR) + j*dane(2:2:2*N_FIR); + else + dane = fread(plik, N_FIR, sample_type); + coefficients.h{ind_resp} = dane; + end + end +end + +fclose(plik); diff --git a/Ex6/matlab - test/readaudiofile.m b/Ex6/matlab - test/readaudiofile.m new file mode 100644 index 0000000000000000000000000000000000000000..825a9a890d520cb59e3dda822abed693cc2fc262 --- /dev/null +++ b/Ex6/matlab - test/readaudiofile.m @@ -0,0 +1,215 @@ +function [x, Fs] = readaudiofile(filename, param) +% [x, Fs] = readaudiofile(filename, param) +% +% returns vector x of the size SIZE=[samples channels]. +% eg. x = x(:,1) + j*x(:,2); +% +% special uses: +% param == 'size': returns SIZE in x +% param == 'cplx': for stereo files returns x as a complex vector instead of matrix +% +% supported file types: +% *.flt +% *.wav +% *.tape +% last modification: 2021.03.29 +% Author: Marek Blok + +return_cplx = 0; +if nargin == 1, + param = inf; +else + if strcmp(param, 'cplx') == 1, + return_cplx = 1; + param = inf; + end +end + +ind = find(filename == '.'); +if length(ind) == 0, + file_type = 'w'; % *.wav +else + ind = ind(end); + + temp = filename(ind+1:end); + + file_type = 'u'; % unknown format + if strcmp(temp, 'flt') == 1, + file_type = 'f'; % two channel floting point + elseif strcmp(temp, 'wav') == 1, + file_type = 'w'; % *.wav + elseif strcmp(temp, 'tape') == 1, + file_type = 't'; % *.tape + end +end + +switch file_type, + case 'w', + if strcmp(param, 'size') == 1, + if exist('audioread','file') == 0 + x = wavread(filename, 'size'); + % siz = [samples channels]. + else + info = audioinfo(filename); + x = [info.TotalSamples, info.NumChannels]; + Fs = info.SampleRate; + end + else + if isfinite(param) + if exist('audioread','file') == 0 + [x, Fs] = wavread(filename, param); + else + if length(param) == 1 + param = [1, param]; + end + [x, Fs] = audioread(filename, param); + end + else + if exist('audioread','file') == 0 + [x, Fs] = wavread(filename); + else + [x, Fs] = audioread(filename); + end + end + end + + case 't' + plik = fopen(filename, 'rb'); + if plik == -1, + error('File does not exist !!!'); + end + + header.size = fread(plik, 1, 'uint32', 0) + 4; + + header.fname = char(fread(plik, 256, 'char', 0).'); + header.cfg_fname = char(fread(plik, 256, 'char', 0).'); + header.sw_rev = fread(plik, 1, 'uint32', 0); + header.hw_rev = fread(plik, 1, 'uint32', 0); + header.file_ = fread(plik, 1, 'uint32', 0); + header.tape_type = fread(plik, 1, 'uint32', 0); + header.start_time = fread(plik, 1, 'int32', 0); % time_t + header.end_time = fread(plik, 1, 'int32', 0); % time_t + + header.total_samples = fread(plik, 1, 'uint32', 0); + file_length = header.total_samples * 4 + header.size + header.current_sample = fread(plik, 1, 'uint32', 0); + header.loop_start = fread(plik, 1, 'int64', 0); + header.loop_end = fread(plik, 1, 'int64', 0); + header.loop = fread(plik, 1, 'int32', 0); + header.group_size_32 = fread(plik, 1, 'uint32', 0); + header.block_size = fread(plik, 1, 'uint32', 0); + header.block_count = fread(plik, 1, 'uint32', 0); + header.fifo_size = fread(plik, 1, 'uint32', 0); + + + header.comment = char(fread(plik, 256, 'char', 0).'); + header.tmp = char(fread(plik, 20, 'char', 0).'); % time_t + header.status = fread(plik, 1, 'uint32', 0); + header.timestamps = fread(plik, 1, 'int32', 0); + header.freq = fread(plik, 1, 'float', 0); + header.cplx_datarate = fread(plik, 1, 'float', 0); + +% ftell(plik) + header.reserved = fread(plik, 128, 'uint32', 0); +% header.reserved.' + + header + ftell(plik) + + header.sample_type = 2; + header.ch_no = 2; + header.Fs = NaN; + + sample_type = 'int16'; + sample_size = 2; + + header_size = header.size; + if strcmp(param, 'size') == 1, + fseek(plik, 0, 'eof'); + size = (ftell(plik) - header_size) / sample_size / header.ch_no; % sizeof(float) *2 + x = size; + else + fseek(plik, header_size, 'bof'); + + len = param(1); + if length(param) > 1, + fseek(plik, sample_size*header.ch_no*(param(1)-1), 'cof'); + len = param(2) - param(1) + 1; + end + +% x = fread(plik, [header.ch_no, len], sample_type); + x = fread(plik, [header.ch_no, len], sample_type, 0); + x = x.'; + end + fclose(plik); + + case 'f' + plik = fopen(filename, 'rb'); + if plik == -1, + error('File does not exist !!!'); + end + + % 3 B - wersja pliku (w tym typ próbek) + % 1 B - liczba kanałów + % 4 B - szybkoć próbkowania + header_size = 8; + header.ver = fread(plik, 1, 'uint8'); + header.sample_type = fread(plik, 1, 'uint16'); + header.ch_no = fread(plik, 1, 'uint8'); + header.Fs = fread(plik, 1, 'uint32'); + + Fs = header.Fs; + + switch (header.ver), + case 0, + switch (header.sample_type), + case 0, + sample_type = 'float'; + sample_size = 4; + case 1, + sample_type = 'uchar'; + sample_size = 1; + case 2, + sample_type = 'short'; + sample_size = 2; + case 3, + sample_type = 'int'; + sample_size = 4; + otherwise + error('Unsupported *.flt sample type !!!'); + end + otherwise + error('Unsupported *.flt file version !!!'); + end + + + if strcmp(param, 'size') == 1, + fseek(plik, 0, 'eof'); + size = (ftell(plik) - header_size) / sample_size / header.ch_no; % sizeof(float) *2 + x = size; + else + len = param(1); + status = 0; + if length(param) > 1, + status = fseek(plik, sample_size*header.ch_no*(param(1)-1), 'cof'); + len = param(2) - param(1) + 1; + end + + if (status == -1) + x = []; + else + x = fread(plik, [header.ch_no, len], sample_type); + x = x.'; + end + end + + fclose(plik); + otherwise + error('Unsupported file format !!!'); +end + +if return_cplx == 1, + if length(x(1,:)) == 2, + x = x(:,1) + j*x(:,2); + end +end \ No newline at end of file diff --git a/Ex6/matlab - test/test_symbol_mapper.m b/Ex6/matlab - test/test_symbol_mapper.m new file mode 100644 index 0000000000000000000000000000000000000000..f09caac38d526fa553a587c8918b2f79493cbbca --- /dev/null +++ b/Ex6/matlab - test/test_symbol_mapper.m @@ -0,0 +1,134 @@ +function test_symbol_mapper(offset) +if nargin == 0, + offset = 0; +end + +[symb, Fp] = fileread('../symbols.flt', 'cplx'); +% % [symb, Fp] = fileread('../symbols.flt'); % dla ASK +% [symb, Fp] = fileread('../ex6_task2_symb_a.flt', 'cplx'); + +% [y{3}, Fp] = fileread('../ex6_task2_symb_c.flt', 'cplx'); + +h = fopen('../Ex6_task1.cpp'); +bits_in = fread(h, 80, 'ubit1', 'ieee-be').'; % MSB first +bits_in2 = fread(h, 80, 'ubit1', 'ieee-le').'; % LSB first +fclose(h); + +% !!! kolejno�� wczytywania bitďż˝w jest odmienna od standardowej!!! +h = fopen('../decoded.txt'); +% h = fopen('../ex6_task2_bina.txt'); +bits_out = fread(h, 80, 'ubit1', 'ieee-be').'; % MSB first // here the bits offset is corrected in the *.cpp +fclose(h); +h = fopen('../decoded_raw.txt'); +% h = fopen('../ex6_task2_bina.txt'); +bits_raw = fread(h, 80, 'ubit1', 'ieee-be').'; % MSB first // here the bits offset is corrected in the *.cpp +fclose(h); +h = fopen('../bits_out.txt'); +bits_out2 = fread(h, 80, 'char').'; +fclose(h); +bits_out2 = double(bits_out2-'0') % For SymbolDemapper this gives 2 additional bits at the begining +% h = fopen('../bits3.flt'); +% bits_out3 = fread(h, 80, 'float').'; +% fclose(h); + +figure(1) +subplot(1,1,1) +plot(symb, 'o') + +bits_out_mod = bits_out; +bits_out_mod(1:2:end) = bits_out(2:2:end); +bits_out_mod(2:2:end) = bits_out(1:2:end); +% bits_out wyprzedza o 2bity (bits_per_symbol) strumienie bits_raw/bits_out2 +figure(2) +subplot(4,1,1) +stem(bits_in, 'b') +subplot(4,1,2) +stem(bits_in2, 'rx') +subplot(4,1,3) +stem(bits_out, 'r') +subplot(4,1,4) +stem(bits_out_mod, 'g') +% subplot(3,1,3) +% stem(bits_raw, 'mx') +% hold on +% stem(bits_out2, 'b+') +% hold off +% % subplot(4,1,4) +% % stem(bits_out3, 'g') + +offset1 = 1 +% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % +% okreďż˝l przyporzďż˝dkowanie bitďż˝w do symboli w mapperze +bits_ = bits_out2; +% bits_ = bits_out; +if offset1 > 0 + y_ref = symb((offset1+1):end); +else + y_ref = symb; +end +if offset1 < 0 + if rem(-offset1, 2) == 0, + bits_ = bits_in((-offset1+1):end); + else + bits_ = bits_in((-offset1+1):end-1); + end +else + bits_ = bits_in; +end +%bin_ref_new = 2*bits_in-1; +bin_ref_new = bits_; + +% \TODO wydobyďż˝ konstelacjďż˝ wejďż˝ciowďż˝ i porďż˝wnaďż˝ jďż˝ z konstelacjďż˝ wyjďż˝ciowďż˝ +symb_ind = 2*bin_ref_new(1:2:end) + bin_ref_new(2:2:end); +figure(23) +subplot(1,2,1) +kolor = 'rmbg'; +scale = [0.9, 0.92, 0.94, 0.96]; +for ind = 0:3 + u{ind+1} = scale(ind+1) * y_ref(find(symb_ind == ind)); + plot(real(u{ind+1}), imag(u{ind+1}), [kolor(ind+1),'o']) + hold on +end +hold off +legend({'00', '10', '01', '11'}) +axis equal +title('mapper constellation') + +% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % +% okreďż˝l przyporzďż˝dkowanie bitďż˝w do symboli w demapperze +bits_ = bits_out2; +% bits_ = bits_out; +if offset > 0 + y_ref = symb((offset+1):end); +else + y_ref = symb; +end +if offset < 0 + if rem(-offset, 2) == 0, + bits_ = bits_((-offset+1):end); + else + bits_ = bits_((-offset+1):end-1); + end +else + bits_ = bits_; +end +%bin_ref_new = 2*bits_in-1; +bin_ref_new = bits_; + +% pierwszy - starszy bit / drugi - mďż˝odszy bit +% % symb_ind = 2*(bin_ref_new(1:2:end)+1)/2 + (bin_ref_new(2:2:end)+1)/2; +symb_ind = 2*bin_ref_new(1:2:end) + bin_ref_new(2:2:end); +figure(23) +subplot(1,2,2) +kolor = 'rmbg'; +scale = [0.9, 0.92, 0.94, 0.96]; +for ind = 0:3 + u{ind+1} = scale(ind+1) * y_ref(find(symb_ind == ind)); + plot(real(u{ind+1}), imag(u{ind+1}), [kolor(ind+1),'o']) + hold on +end +hold off +legend({'00', '10', '01', '11'}) +axis equal +title('demapper constellation') + diff --git a/Ex6/matlab - test/test_task1.m b/Ex6/matlab - test/test_task1.m new file mode 100644 index 0000000000000000000000000000000000000000..e7bd8f652da0a8023f8e2edf7c0b6b2f52077a27 --- /dev/null +++ b/Ex6/matlab - test/test_task1.m @@ -0,0 +1,143 @@ +function test_task1(ind) +if nargin == 0, + ind = 2*(3+2*4); +end +[x, Fp] = fileread('../ex6_task1.flt', 'cplx'); +[y, Fp_y] = fileread('../ex6_task1b.flt', 'cplx'); +[y_ref, Fp] = fileread('../ex6_task1a.flt', 'cplx'); + +channel_no = 1; + +% size(x) +% size(y) +% figure +% plot(x-y) +% return + +N_DFT = 32; M = 8; +Fsymb = Fp_y/N_DFT +% K = 32; M = 8; + +figure(1) +subplot(2,1,1) +plot(x) +axis equal +subplot(2,2,3) +plot(y, 'b') +axis equal +subplot(2,2,4) +plot(y_ref, 'b.') +axis equal +% % plot(y) + +% y = y(21+1:end); + +figure(2) +subplot(2,1,1) +stem(real(y)/max(abs(y)), 'b') +hold on +stem(real(y_ref), 'r') +hold off +subplot(2,1,2) +[c, l] = xcorr(real(y), real(y_ref)); +plot(l, c) + +figure(3) +subplot(2,1,1) +stem(imag(y)/max(abs(y)), 'b') +hold on +stem(imag(y_ref), 'r') +hold off +subplot(2,1,2) +[c, l] = xcorr(imag(y), imag(y_ref)); +plot(l, c) + +n = [0:length(y)-1].'; +het = exp(-j*channel_no*(2*pi)/N_DFT*n); +% het = 1; +figure(13) +K_psd = 1024; +F_psd = linspace(-Fp_y/2, Fp_y/2, 2*K_psd+1); F_psd(end) = []; +% psd(het.*y) +Pxx = pwelch(het.*y, blackman(K_psd), ceil(0.5*K_psd), F_psd, Fp_y); +Pxx_dB = 10*log10(Pxx); +Pxx_dB_max = ceil(max(Pxx_dB)/3)*3; Y_range = [-100, 0]+Pxx_dB_max; +plot([0,0], Y_range, 'r--'); +set(gca, 'Xlim', [-Fp_y/2, Fp_y/2]/1000, 'Ylim', Y_range); +hold on +for ind = 1:ceil(N_DFT/2), + plot(ind*Fsymb/1000*[1,1], Y_range, 'r--'); + plot(-ind*Fsymb/1000*[1,1], Y_range, 'r--'); + + plot((ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); + plot(-(ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); +end +plot(F_psd/1000,Pxx_dB); +hold off + +% % return +% xt{1} = ((-1).^(1:length(y)).').*y/max(abs(y)); +xt{1} = het.*y/max(abs(y)); +xt{2} = y_ref; +trajekt_(xt, 10, 1:M:length(y), 0, 1) +tmp = xt{1}; +eyediagram2(tmp(1:100*M), 0, M, 15); +pause + +y = y(1:M:end); +% y = y.*exp(j*pi/4); +s_re = real(y) > 0; +s_im = imag(y) > 0; + +bin = zeros(1,2*length(s_re)); +bin(1:2:end) = s_re; +bin(2:2:end) = s_im; + +bits_per_char = 8; +%ind = 0; + +h = fopen('../Ex6_task1.cpp'); +bin2 = double(fread(h, 800, 'ubit1')).'; +fclose(h) +% +figure(4) +subplot(2,1,1) +[c, l] = xcorr(s_re, bin2(1:2:end)); +plot(l, c) +subplot(2,1,2) +[c, l] = xcorr(s_re, bin2(2:2:end)); +plot(l, c) + +% return +figure(5) +subplot(2,1,1) +stem(bin, 'b') +hold on +stem(bin2, 'r') +hold off +subplot(2,1,2) +[c, l] = xcorr(bin, bin2); +plot(l, c) + +% bin = bin_root.' +bin = bin((ind+1):end); +% for ind= 1:bits_per_char, +for ind= 1, + bin_ = bin(ind:end); + B = floor(length(bin_)/bits_per_char)*bits_per_char; + + tekst = bin_(1:bits_per_char:B); + for bit = 1:bits_per_char-1, +% tekst = tekst + (2^bit)*bin_((bit+1):bits_per_char:B); + tekst = tekst + (2^((bits_per_char-1)-bit))*bin_((bit+1):bits_per_char:B); + end + char(tekst) +end + + +% +% B_pre = length(pre)*bits_per_char; +% bin_pre = zeros(B_pre,1); +% for ind = 0:bits_per_char-1, +% bin_pre((ind+1):bits_per_char:end) = rem(floor(pre / (2^ind)),2); +% end diff --git a/Ex6/matlab - test/test_task1_signal.m b/Ex6/matlab - test/test_task1_signal.m new file mode 100644 index 0000000000000000000000000000000000000000..ea619fbe391a262f5328319379dc357548489e86 --- /dev/null +++ b/Ex6/matlab - test/test_task1_signal.m @@ -0,0 +1,304 @@ +function test_task1_signal(mode,matched, offset_0, phase_offset) +% test_task1_signal('zad1', 'srRC', 0, -2*pi/32) +% test_task1_signal('zad1b', 'srRC', 0, -2*pi/32) +% +% offset_0 - przesuniďż˝cie w prďż˝bkach chwili prďż˝bkowania sybmoli +% phase_offset - korekta fazy poczďż˝tkowej heterodynny + +channel_no = 1; +% channel_no = 4; +% channel_no = 6; + + +% if nargin == 0, +% ind = 2*(3+2*4); +% end + +if nargin == 0 + % matched = 'srRC' + matched = 'rect' + % mode = 'zad1' + mode = 'zad1' + + offset_0 = 0; + phase_offset = 0; +elseif nargin == 2 + offset_0 = 0; + phase_offset = 0; +elseif nargin == 3 + phase_offset = 0; +end + + + +N_DFT = 32; M = 8; +N_symb = N_DFT; + +switch mode + case 'zad1', + [x, Fp] = fileread('../ex6_task1.flt', 'cplx'); + offset = 5*N_symb + offset_0; + case 'zad1b', + [x, Fp] = fileread('../ex6_task1b.flt', 'cplx'); + offset = 6*N_symb + offset_0; +end +% % [y_ref, Fp] = fileread('../ex6_task1a.flt', 'cplx'); + +[x_ch1, F_symb] = fileread('../ex6_task1_ch1.flt', 'cplx'); + + +switch matched, + case 'rect', + h_matched = ones(1,N_DFT); % duďż˝y poziom interferencji ISI/ICI + offset = offset + 0.5*N_symb; + + case 'srRC', + [coef, ver] = load_filter_coef('../matlab/ex6_task1_h_rc.coef') + h_rc = coef.h; + F_symb = coef.Fp; + + h_matched = N_symb*h_rc; + offset = offset + 5*N_symb; +end +N_matched = length(h_matched); + +K_psd = 1024; +N_symb = N_DFT + +figure(11) +subplot(2,1,1) +n = 0:N_matched-1; +plot(n-(N_matched-1)/2, h_matched) +hold on +n = 0:(2*N_matched-1)-1; +plot(n-2*(N_matched-1)/2, conv(h_matched,h_matched), 'r') +plot([-N_matched, N_matched], [0, 0], 'k') +hold off +set(gca,'xlim', [-N_matched, N_matched], 'xtick', N_symb*[-16:16], 'xgrid', 'on') + +subplot(2,1,2) +[H, F_h] = freqz(h_matched, 1, K_psd, F_symb*N_DFT); +[H2, F_h] = freqz(conv(h_matched, h_matched), 1, K_psd, F_symb*N_DFT); +plot(F_h, 20*log10(abs(H))) +hold on +plot(F_h, 20*log10(abs(H2)), 'r') +hold off +set(gca, 'xlim', [0, F_symb/2*N_DFT], 'xtick', [0:N_DFT]*F_symb/2, 'xgrid', 'on') +set(gca, 'Ylim', [-170, 3]) + +% channel_no = 1; + +% size(x) +% size(y) +% figure +% plot(x-y) +% return + +Fsymb = Fp/N_DFT +% K = 32; M = 8; + +figure(1) +subplot(4,1,1) +plot(real(x), 'b') +title('wygenerowany sygnaďż˝ wielokanaďż˝owy') +%axis equal +subplot(4,1,2) +plot(imag(x), 'r') +%axis equal + +subplot(2,1,2) +F_psd = linspace(-Fp/2, Fp/2, 2*K_psd+1); F_psd(end) = []; +% psd(het.*y) +Pxx = pwelch(x, blackman(K_psd), ceil(0.5*K_psd), F_psd, Fp); +Pxx_dB = 10*log10(Pxx); +Pxx_dB_max = ceil(max(Pxx_dB)/3)*3; Y_range = [-100, 0]+Pxx_dB_max; +plot([0,0], Y_range, 'r--'); +set(gca, 'Xlim', [-Fp/2, Fp/2]/1000, 'Ylim', Y_range); +hold on +for ind = 1:ceil(N_DFT/2), + plot(ind*Fsymb/1000*[1,1], Y_range, 'r--'); + plot(-ind*Fsymb/1000*[1,1], Y_range, 'r--'); + + plot((ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); + plot(-(ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); +end +plot(F_psd/1000,Pxx_dB); +hold off +k = [-ceil(N_DFT/2):ceil(N_DFT/2)]'; +ticks = k*Fsymb/1000; +set(gca, 'xtick', ticks, 'xticklabel', num2str(k)) + + +% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % +n = [0:length(x)-1].'; +het = exp(-j*channel_no*(2*pi)/N_DFT*n + j*phase_offset); +y = het .* x; + +figure(2) +subplot(4,1,1) +plot(real(y), 'b') +title('sygnaďż˝ heterodynowany w dďż˝') +%axis equal +subplot(4,1,2) +plot(imag(y), 'r') +%axis equal + +subplot(2,1,2) +Pxx = pwelch(y, blackman(K_psd), ceil(0.5*K_psd), F_psd, Fp); +Pxx_dB = 10*log10(Pxx); +Pxx_dB_max = ceil(max(Pxx_dB)/3)*3; Y_range = [-100, 0]+Pxx_dB_max; +plot([0,0], Y_range, 'r--'); +set(gca, 'Xlim', [-Fp/2, Fp/2]/1000, 'Ylim', Y_range); +hold on +for ind = 1:ceil(N_DFT/2), + plot(ind*Fsymb/1000*[1,1], Y_range, 'r--'); + plot(-ind*Fsymb/1000*[1,1], Y_range, 'r--'); + + plot((ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); + plot(-(ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); +end +plot(F_psd/1000,Pxx_dB); +hold off +k = [-ceil(N_DFT/2):ceil(N_DFT/2)]'; +ticks = k*Fsymb/1000; +set(gca, 'xtick', ticks, 'xticklabel', num2str(k)) + +% y = y(21+1:end); +y2 = filter(h_matched, 1, y); + +figure(3) +subplot(4,1,1) +plot(real(y2), 'b') +title('sygnaďż˝ heterodynowany w dďż˝ po filtrze dopasowanym') +%axis equal +subplot(4,1,2) +plot(imag(y2), 'r') +%axis equal + +subplot(2,1,2) +Pxx = pwelch(y2, blackman(K_psd), ceil(0.5*K_psd), F_psd, Fp); +Pxx_dB = 10*log10(Pxx); +Pxx_dB_max = ceil(max(Pxx_dB)/3)*3; Y_range = [-100, 0]+Pxx_dB_max; +plot([0,0], Y_range, 'r--'); +set(gca, 'Xlim', [-Fp/2, Fp/2]/1000, 'Ylim', Y_range); +hold on +for ind = 1:ceil(N_DFT/2), + plot(ind*Fsymb/1000*[1,1], Y_range, 'r--'); + plot(-ind*Fsymb/1000*[1,1], Y_range, 'r--'); + + plot((ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); + plot(-(ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); +end +plot(F_psd/1000,Pxx_dB); +hold off +k = (-ceil(N_DFT/2):ceil(N_DFT/2))'; +ticks = k*Fsymb/1000; +set(gca, 'xtick', ticks, 'xticklabel', num2str(k)) + +m = 0:length(x_ch1)-1; +figure(3) +subplot(4,1,1) +hold on +plot(N_DFT*m+offset, real(x_ch1), 'k'); +hold off +subplot(4,1,2) +hold on +plot(N_DFT*m+offset, imag(x_ch1), 'k'); +hold off + +symbols = y2(offset:N_symb:end); +figure(4) +plot(symbols, 'b.') +axis equal + +return + +% figure(2) +% subplot(2,1,1) +% stem(real(x)/max(abs(x)), 'b') +% hold on +% stem(real(y_ref), 'r') +% hold off +% subplot(2,1,2) +% [c, l] = xcorr(real(y), real(y_ref)); +% plot(l, c) +% +% figure(3) +% subplot(2,1,1) +% stem(imag(y)/max(abs(y)), 'b') +% hold on +% stem(imag(y_ref), 'r') +% hold off +% subplot(2,1,2) +% [c, l] = xcorr(imag(y), imag(y_ref)); +% plot(l, c) +% +% % het = 1; +% figure(13) +% +% % % return +% % xt{1} = ((-1).^(1:length(y)).').*y/max(abs(y)); +% xt{1} = het.*y/max(abs(y)); +% xt{2} = y_ref; +% trajekt_(xt, 10, 1:M:length(y), 0, 1) +% tmp = xt{1}; +% eyediagram2(tmp(1:100*M), 0, M, 15); +% pause +% +% y = y(1:M:end); +% % y = y.*exp(j*pi/4); +% s_re = real(y) > 0; +% s_im = imag(y) > 0; +% +% bin = zeros(1,2*length(s_re)); +% bin(1:2:end) = s_re; +% bin(2:2:end) = s_im; +% +% bits_per_char = 8; +% %ind = 0; +% +% h = fopen('../Ex6_task1.cpp'); +% bin2 = double(fread(h, 800, 'ubit1')).'; +% fclose(h) +% % +% figure(4) +% subplot(2,1,1) +% [c, l] = xcorr(s_re, bin2(1:2:end)); +% plot(l, c) +% subplot(2,1,2) +% [c, l] = xcorr(s_re, bin2(2:2:end)); +% plot(l, c) +% +% % return +% figure(5) +% subplot(2,1,1) +% stem(bin, 'b') +% hold on +% stem(bin2, 'r') +% hold off +% subplot(2,1,2) +% [c, l] = xcorr(bin, bin2); +% plot(l, c) +% +% % bin = bin_root.' +% bin = bin((ind+1):end); +% % for ind= 1:bits_per_char, +% for ind= 1, +% bin_ = bin(ind:end); +% B = floor(length(bin_)/bits_per_char)*bits_per_char; +% +% tekst = bin_(1:bits_per_char:B); +% for bit = 1:bits_per_char-1, +% % tekst = tekst + (2^bit)*bin_((bit+1):bits_per_char:B); +% tekst = tekst + (2^((bits_per_char-1)-bit))*bin_((bit+1):bits_per_char:B); +% end +% char(tekst) +% end +% +% +% % +% % B_pre = length(pre)*bits_per_char; +% % bin_pre = zeros(B_pre,1); +% % for ind = 0:bits_per_char-1, +% % bin_pre((ind+1):bits_per_char:end) = rem(floor(pre / (2^ind)),2); +% % end diff --git a/Ex6/matlab - test/test_task1b_new.m b/Ex6/matlab - test/test_task1b_new.m new file mode 100644 index 0000000000000000000000000000000000000000..70b10163e6149ad34a798afa31b8d0ccf3b74834 --- /dev/null +++ b/Ex6/matlab - test/test_task1b_new.m @@ -0,0 +1,84 @@ +function test_task1b_new + +x{1} = fileread('../ex6_task1a.flt', 'cplx'); +x_new{1} = fileread('../ex6_task1a_new.flt', 'cplx'); + +[bin_ref, Fp] = fileread('../ex6_task1_bin_a.flt', 'cplx'); +[bin_ref_new, Fp] = fileread('../ex6_task1_bin_a_new.flt'); % real +B = length(bin_ref_new); +if rem(B,2) == 1, + disp('rem(B,2) == 1'); + bin_ref_new = bin_ref_new(1:end-1); +end + +figure(1) +subplot(2,2,1) +plot(x{1}, 'bo') +axis equal +subplot(2,2,2) +plot(x_new{1}, 'ro') +axis equal +title('Wejďż˝ciowe strumienie symboli dla kanaďż˝u nr 1') + +figure(2) +subplot(2,1,1) +plot(real(x{1}), 'b') +hold on +plot(real(x_new{1}), 'r') +hold off +set(gca, 'Xlim', [0,100], 'Ylim', [-1.1, 1.1]) +subplot(2,1,2) +plot(imag(x{1}), 'b') +hold on +plot(imag(x_new{1}), 'r') +hold off +set(gca, 'Xlim', [0,100], 'Ylim', [-1.1, 1.1]) + +% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % +% okreďż˝l przyporzďż˝dkowanie bitďż˝w do symboli modulatorze +y_ref = x{1}; +% real - starszy bit / imag - mďż˝odszy bit +symb_ind = (2*real(bin_ref)+1)/2 + (imag(bin_ref)+1)/2; +figure(23) +subplot(1,2,1) +kolor = 'rmbg'; +scale = [0.9, 0.92, 0.94, 0.96]; +for ind = 0:3 + u{ind+1} = scale(ind+1) * y_ref(find(symb_ind == ind)); + plot(real(u{ind+1}), imag(u{ind+1}), [kolor(ind+1),'o']) + hold on +end +hold off +legend({'00', '01', '10', '11'}) +axis equal + +figure(23) +subplot(1,2,2) +% (2+8+1+1)*2 - wyprzedzenie bin data w bitach +% +% bin_ref_new = bin_ref_new(3:end); +% +++++++++++++++++++++++++++++++++++++++++++ +% Bez skip w kodzie 2 bity z przedo za duďż˝o +prefix_size = 2; +bin_ref_new = [-ones(prefix_size,1); bin_ref_new(1:end-prefix_size)]; +% % Przy skip w kodzie powinno byďż˝ (2+8+1+1)*2-2 bitďż˝w za maďż˝o - nie ten kod +% prefix_size = (2+8+1)*2; +% bin_ref_new = bin_ref_new(prefix_size+1:end); +y_ref = x_new{1}; +% % bin_ref_new = bin_ref_new(2:end-1); +% bin_ref_new = bin_ref_new(3:end); + +% pierwszy - starszy bit / drugi - mďż˝odszy bit +symb_ind = 2*(bin_ref_new(1:2:end)+1)/2 + (bin_ref_new(2:2:end)+1)/2; +kolor = 'rmbg'; +scale = [0.9, 0.92, 0.94, 0.96]; +hp = []; +for ind = 0:3 + u{ind+1} = scale(ind+1) * y_ref(find(symb_ind == ind)); + hp(ind+1)=plot(real(u{ind+1}), imag(u{ind+1}), [kolor(ind+1),'o']); + hold on +end +hold off +legend(hp, {'00', '01', '10', '11'}) +axis equal + diff --git a/Ex6/matlab - test/test_task2.m b/Ex6/matlab - test/test_task2.m new file mode 100644 index 0000000000000000000000000000000000000000..c42155b6e290c940577fc8d13fa6043ec4659054 --- /dev/null +++ b/Ex6/matlab - test/test_task2.m @@ -0,0 +1,170 @@ +function test_task2(symb_offset,phase_offset, bit_offset) +% test_task2(2+4*ile, 0) +% test_task2(10, 0) - % peďż˝na kompensacja opďż˝nienia +% demoulacja pierwszego podkanaďż˝u +% test_task2(10,0,2) dla ex6_task1.flt +% test_task2(11,0,2) dla ex6_task1b.flt +if nargin == 0, +% ind = 2*(3+2*4); + symb_offset = 10; + phase_offset = 0; + bit_offset = 2; +end +phasor = exp(j*phase_offset); + +% dla ex6_task1.flt +[x, Fp] = fileread('../ex6_task1.flt'); +[y, Fp] = fileread('../ex6_task2_symb_A_a.flt', 'cplx'); +% dla ex6_task1b.flt +[x, Fp] = fileread('../ex6_task1b.flt'); +[y, Fp] = fileread('../ex6_task2_symb_B_a.flt', 'cplx'); + +[y_ref, Fp] = fileread('../ex6_task1a.flt', 'cplx'); + +y = y(symb_offset+1:end); + +% y = y_ref; % - przesuniďż˝cie o 4 + + +mode = 'QPSK_A', % 00 - 1, 10 - j, 01 - -j, 11 - -1 + +% % y = y - (0.5+j*0.5); +% y_ref = (real(y_ref) > 0) + j*(imag(y_ref) > 0); +K = 32; M = 32; +% K = 16; M = 8; + +figure(1) +subplot(2,1,1) +plot(x) +subplot(2,2,3) +plot(y, 'b.') +axis equal +subplot(2,2,4) +plot(y_ref, 'b.') +axis equal +% % plot(y) + +% return + +figure(2) +subplot(2,1,1) +stem(real(y)/max(abs(y)), 'b') +hold on +stem(real(y_ref), 'r') +hold off +subplot(2,1,2) +[c, l] = xcorr(real(y), real(y_ref)); +plot(l, c) + +figure(3) +subplot(2,1,1) +stem(imag(y)/max(abs(y)), 'b') +hold on +stem(imag(y_ref), 'r') +hold off +subplot(2,1,2) +[c, l] = xcorr(imag(y), imag(y_ref)); +plot(l, c) + +n = [0:length(y)-1].'; +% het = exp(-j*(8)*pi/(32)*n); +het = 1; +% het = 1; +figure(13) +psd(het.*y) +% return + +if M < K + % xt{1} = ((-1).^(1:length(y)).').*y/max(abs(y)); + xt{1} = het.*y/max(abs(y)); + xt{2} = y_ref; + trajekt_(xt, 10, 1:M:length(y), 0, 1) + tmp = xt{1}; + eyediagram2(tmp(1:100*M), 0, M, 15); + pause + + y = y(1:M:end); +end +y = y * phasor; + + +switch mode, + case 'QPSK_A', % ďż˝le ? % 00 - 1, 10 - j, 01 - -j, 11 - -1 + % 00 - 1, 01 - j, 10 - -j, 11 - -1 + for n = 1:length(y), + if real(y(n)) > abs(imag(y(n))), % 1 + s_re(n) = 0.0; + s_im(n) = 0.0; + elseif -real(y(n)) > abs(imag(y(n))), % -1 + s_re(n) = 1.0; + s_im(n) = 1.0; + elseif imag(y(n)) > abs(real(y(n))), % j + s_re(n) = 1.0; s_im(n) = 0.0; +% s_re(n) = 0.0; s_im(n) = 1.0; + elseif -imag(y(n)) > abs(real(y(n))), % -j + s_re(n) = 0.0; s_im(n) = 1.0; +% s_re(n) = 1.0; s_im(n) = 0.0; + else + s_re(n) = 0.0; + s_im(n) = 0.0; + end + end + + case 'QPSK_B', % ??? + s_re = real(y) > 0; + s_im = imag(y) > 0; +end + + +bin = zeros(1,2*length(s_re)); +bin(1:2:end) = s_re; +bin(2:2:end) = s_im; + +bits_per_char = 8; +%ind = 0; + +h = fopen('../Ex6_task1.cpp'); +bin2 = fread(h, 800, 'ubit1', 'ieee-be').'; % ??? +fclose(h); +% +figure(4) +subplot(2,1,1) +[c, l] = xcorr(s_re, bin2(1:2:end)); +plot(l, c) +subplot(2,1,2) +[c, l] = xcorr(s_re, bin2(2:2:end)); +plot(l, c) + +% return +figure(5) +subplot(2,1,1) +stem(bin, 'b') +hold on +stem(bin2, 'rx') +hold off +subplot(2,1,2) +[c, l] = xcorr(bin, bin2); +plot(l, c) + +% % bin = bin_root.' +% bin = bin((ind+1):end); +% for ind= 1:bits_per_char, +for ind= bit_offset+1, + bin_ = bin(ind:end); + B = floor(length(bin_)/bits_per_char)*bits_per_char; + + tekst = bin_(1:bits_per_char:B); + for bit = 1:bits_per_char-1, +% tekst = tekst + (2^bit)*bin_((bit+1):bits_per_char:B); + tekst = tekst + (2^((bits_per_char-1)-bit))*bin_((bit+1):bits_per_char:B); + end + char(tekst) +end + + +% +% B_pre = length(pre)*bits_per_char; +% bin_pre = zeros(B_pre,1); +% for ind = 0:bits_per_char-1, +% bin_pre((ind+1):bits_per_char:end) = rem(floor(pre / (2^ind)),2); +% end diff --git a/Ex6/matlab - test/test_task2_constelation.m b/Ex6/matlab - test/test_task2_constelation.m new file mode 100644 index 0000000000000000000000000000000000000000..3c9f5dc7469614aaf7501bfdf940c8c2923581b7 --- /dev/null +++ b/Ex6/matlab - test/test_task2_constelation.m @@ -0,0 +1,115 @@ +function test_task2_constelation(offset) +% test konstelacji dla wszystkich podkana��w +% * test_task2_constelation(2) - dla demodulacji ex6_task1.flt [mode = 'A'] +% * test_task2_constelation(3) - dla demodulacji ex6_task1b.flt [mode = 'B'] +if nargin == 0, + offset = 0; +end + +mode = 'A' % demodulacja ex6_task1.flt +% mode = 'B' % demodulacja ex6_task1b.flt + +% Surowe symbole z poszczegďż˝lnych wyodrďż˝bnionych podkanaĹĂłw Ex6_task_zad2.cpp +[y{1}, Fp] = fileread(['../ex6_task2_symb_', mode, '_a.flt'], 'cplx'); +[y{2}, Fp] = fileread(['../ex6_task2_symb_', mode, '_b.flt'], 'cplx'); +[y{3}, Fp] = fileread(['../ex6_task2_symb_', mode, '_c.flt'], 'cplx'); + +% symbole wyjďż˝ciowe mappera symboli dla pierwszego podkanaĹĂłw Ex6_taskzad1.cpp +%x{1} = fileread('../ex6_task1_ch1.flt', 'cplx'); + +% symbole wyjďż˝ciowe mappera symboli dla pierwszego podkanaĹĂłw Ex6_taskzad1b.cpp +x{1} = fileread('../ex6_task1a_new.flt', 'cplx'); + +y{1} = y{1}((9+1):end); + +bin_ref = fileread('../ex6_task1_bin_a.flt', 'cplx'); + +h = fopen('../ex6_task2_bina.txt'); +bits_out = fread(h, inf, 'ubit1', 'ieee-be').'; +fclose(h); +h = fopen('../ex6_task2_bina.flt'); +bits_out2 = fread(h, inf, 'float').'; +fclose(h); + +N = length(y{1}); +M = length(x{1}); + +figure(1) +subplot(2,3,1) +plot(y{1}, 'b.') +axis equal +subplot(2,3,2) +plot(y{2}, 'b.') +axis equal +subplot(2,3,3) +plot(y{3}, 'b.') +axis equal + +subplot(2,3,4) +plot(x{1}, 'b.') +axis equal + +figure(2) +subplot(2,1,1) +plot(real(y{1}), 'b') +hold on +plot(real(x{1}), 'r') +hold off +subplot(2,1,2) +plot(imag(y{1}), 'b') +hold on +plot(imag(x{1}), 'r') +hold off + + +N1 = min([N,M]); +figure(3) +subplot(2,3,1) +plot(y{1}.*conj(x{1}(1:N1)), 'r.') +axis equal +subplot(2,1,2) +[c, l] = xcorr(y{1}, x{1}(1:N1)); +plot(l, real(c), 'b'); +hold on +plot(l, imag(c), 'r'); +hold off + +% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % +% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % +symb = y{1}; +bits_ = bits_out; +if offset > 0 + y_ref = symb((offset+1):end); +else + y_ref = symb; +end +if offset < 0 + if rem(-offset, 2) == 0, + bits_in = bits_((-offset+1):end); + else + bits_in = bits_((-offset+1):end-1); + end +else + bits_in = bits_; +end +%bin_ref_new = 2*bits_in-1; +bin_ref_new = bits_in; + +if length(bin_ref_new) > 2*length(y_ref), + bin_ref_new = bin_ref_new(1:2*length(y_ref)); +end +% pierwszy - starszy bit / drugi - mďż˝odszy bit +% % symb_ind = 2*(bin_ref_new(1:2:end)+1)/2 + (bin_ref_new(2:2:end)+1)/2; +symb_ind = 2*bin_ref_new(1:2:end) + bin_ref_new(2:2:end); +figure(3) +subplot(2,3,3) +kolor = 'rmbg'; +scale = [0.9, 0.92, 0.94, 0.96]; +for ind = 0:3 + u{ind+1} = scale(ind+1) * y_ref(find(symb_ind == ind)); + plot(real(u{ind+1}), imag(u{ind+1}), [kolor(ind+1),'o']) + hold on +end +hold off +legend({'00', '10', '01', '11'}) +axis equal diff --git a/Ex6/matlab - test/trajekt_.m b/Ex6/matlab - test/trajekt_.m new file mode 100644 index 0000000000000000000000000000000000000000..696ad413ada22898d471568f86e4b380856865ea --- /dev/null +++ b/Ex6/matlab - test/trajekt_.m @@ -0,0 +1,403 @@ +function klawisz=trajekt_(x_s, time, punkty, no_head, pause_on_start) +% \added 2019.10.18 żółte tło okna w trybie pauzy +% \added 2019.10.18 prosty tryb testowania (ustawiajšc test_mode = 1; w linii 11) +% \fixed 2019.10.18 wyłšczenie zoom przed definiowaniem 'KeyPressFcn' +% \added 2017.10.24 dodano obsługę trybu, w którym x_s zawiera dane i +% etykiety (interpreter LaTeX): x_s{1}.data = ..., x_s{1}.label = '?'; +% \fixed 2015.10.27 addapted to matlab 2015b +% \fixed 2006.11.14 fixed axes position in single and double plot mode +% \added 2006.11.14 input option no_head +% \added 2006.11.14 input option pause_on_start + +test_mode = 0; +if (test_mode == 1) & (nargin == 0) + x_s = exp(sqrt(-1)*0.1*[0:1000]); + klawisz = trajekt_(x_s, 100, [], 0, 1); + return; +end + +if nargin < 5, + pause_on_start = 0; +end +if nargin < 4, + no_head = 1; +end +if nargin < 3, + punkty = []; +end +labels = {}; + + +if nargin == 0, +error('Not enough input arguments.'); +else + if isreal(x_s), + ile = 1; + x = x_s; + y = x; x = 1:length(y); + + clear x_s; + x_s{1}.x = x; + x_s{1}.y = y; + max1=max(abs(x(~isinf(x)))); + zakres=[-max1 max1 -max1 max1]; + else + if iscell(x_s) == 0, + ile = 1; + y=imag(x_s); + x=real(x_s); + clear x_s; + x_s{1}.x = x; + x_s{1}.y = y; + max1=max(max(abs(x(~isinf(x)))),max(abs(y(~isinf(y))))); + zakres=[-max1 max1 -max1 max1]; + else + x_s_tmp = x_s; + clear x_s + + ile = length(x_s_tmp); + max1 = eps; + for ind = 1:ile, + if isfield(x_s_tmp{ind}, 'label'), + labels{ind} = x_s_tmp{ind}.label; + end + if isfield(x_s_tmp{ind}, 'data'), + x_s_tmp{ind} = x_s_tmp{ind}.data; + end + x=real(x_s_tmp{ind}); + y=imag(x_s_tmp{ind}); + x_s{ind}.x = x; + x_s{ind}.y = y; + + max1=max([max(abs(x(~isinf(x)))),max(abs(y(~isinf(y)))), max1]); + end + zakres=[-max1 max1 -max1 max1]; + end + end +end + + +% if nargin == 0, +% error('Not enough input arguments.'); +% else +% if isreal(x), +% % y = x; x = 1:length(y); +% % max_y=max(abs(x(~isinf(x)))); +% % min_y=-max_x; +% % max_x=length(y); +% % min_x=1; +% y = x; x = 1:length(y); +% max_y=10; +% min_y=-10; +% max_x=10; +% min_x=10; +% else +% % y=imag(x); +% % x=real(x); +% % max_x=max(abs(x(~isinf(x)))); +% % min_x=-max_x; +% % max_y=max_x; +% % min_y=-max_x; +% y=imag(x); +% x=real(x); +% max_x=10; +% min_x=-10; +% max_y=10; +% min_y=-10; +% +% end +% end +if nargin==1, + time=0.01; +else + time=time/length(x); +end +p = 0.02; + +hf=findobj(0,'tag','trajektoria'); +if isempty(hf) + hf=figure('NextPlot','add', 'Color', 'w'); +else + figure(hf); +end +set(hf,'tag','trajektoria', 'Name', 'Trajektorie - dr inż. Marek Blok (14.11.2006)'); +delete(findobj(hf,'type', 'axes')); + +global klawisz +global trajekt_hf + +if pause_on_start == 0, + klawisz = '~'; % od razu ruszaj +else + klawisz = 13; % pauza na starcie +end +trajekt_hf = hf; +% set(hf, 'KeyPressFcn', 'global klawisz; fig = get(0, ''PointerWindow''); if fig ~= 0, klawisz = get(fig,''CurrentCharacter''); if isempty(klawisz), klawisz = ''~''; end; end;'); +zoom(hf, 'off'); +set(hf, 'KeyPressFcn', 'global klawisz; global trajekt_hf, fig = trajekt_hf; if fig ~= 0, klawisz = get(fig,''CurrentCharacter''); if isempty(klawisz), klawisz = ''~''; end; end;'); + +min_x=-5; +max_x=5; +min_y=-5; +max_y=5; + +if ile == 1, + ax{1}=axes('Units','Normalized','Position',[0, 0, 1, 1]+ [1, 1, -2, -2]*0.05); + if ~ishold, + axis([min_x max_x ... + min_y max_y]/5) + axis square + end + if length(labels) >= 1 + title(labels{1}, 'Interpreter', 'latex'); + end + set(ax{1},'Xtick', -1:0.5:1, 'Ytick', -1:0.5:1, 'Xgrid','on','Ygrid','on'); + co = get(ax{1},'colororder'); +elseif ile == 2, + for ind = 0:1, + ax{ind+1}=axes('Units','Normalized','Position',[ind*0.5 0 [0.5 1]]+ [1, 1, -2, -2]*0.05); + if ~ishold, + axis([min_x max_x ... + min_y max_y]/5) + axis square + end + if length(labels) >= 2 + title(labels{ind+1}, 'Interpreter', 'latex'); + end + set(ax{ind+1},'Xtick', -1:0.5:1, 'Ytick', -1:0.5:1, 'Xgrid','on','Ygrid','on'); + end + co = get(ax{1},'colororder'); +else + % 12 + for ind = 0:ile-1, + % 4 x 3 + x_ = rem(ind,4); + y_ = (ind-x_)/4; + ax{ind+1}=axes('Units','Normalized','Position',[x_*0.25 1-(y_+1)*0.33 [0.25 0.33]*0.95]); + if ~ishold, + axis([min_x max_x ... + min_y max_y]/5) + axis square + end + if length(labels) >= ile + title(labels{ind+1}, 'Interpreter', 'latex'); + end + set(ax{ind+1},'Xtick', -1:0.5:1, 'Ytick', -1:0.5:1, 'Xgrid','on','Ygrid','on'); + end + co = get(ax{1},'colororder'); +end + + + +m = length(x_s{1}.x); +for ind = 1:ile-1, + if m == 0, + m = length(x_s{ind+1}.x); + else + m = min([m, length(x_s{ind+1}.x)]); + end +end +k = ceil(p*m); + +%ax1 = newplot; +% ax1=axes('Units','Pixels','Position',[30 30 300 300]); + +% ax2=axes('Units','Pixels','Position',[360 30 300 300]); +for ind = 0:ile-1, + axes(ax{ind+1}) + + % Choose first three colors for head, body, and tail + if no_head == 0, + head{ind+1} = line('color',co(1,:),'linestyle', 'none', 'marker', 'o', 'linewidth', 3, ... + 'xdata',x_s{ind+1}.x(1),'ydata',x_s{ind+1}.y(1)); + end + %body_x = []; body_y = []; + body_x{ind+1} = NaN*ones(1,m); body_y{ind+1} = NaN*ones(1,m); + body_x{ind+1}(1) = x_s{ind+1}.x(1); body_y{ind+1}(1) = x_s{ind+1}.y(1); + body{ind+1} = line('color','b','linestyle','-', 'marker','none', ... %'erase','none', ... + 'xdata',body_x{ind+1},'ydata',body_y{ind+1}); + %tail_x = []; tail_y = []; + tail_x{ind+1} = NaN*ones(1,k+1); tail_y{ind+1} = NaN*ones(1,k+1); + tail_x{ind+1}(1) = x_s{ind+1}.x(1); tail_y{ind+1}(1) = x_s{ind+1}.y(1); + tail{ind+1} = line('color','r','linestyle','-','marker','none', ... %'erase','none', ... + 'xdata',tail_x{ind+1},'ydata',tail_y{ind+1}); + if no_head == 0, + head2{ind+1} = line('color',co(4,:),'linestyle', 'none', 'marker','o', 'linewidth', 3, ... + 'xdata',x_s{ind+1}.x(1),'ydata',x_s{ind+1}.y(1)); + end + + if ile > 2, + h_punkty{ind+1} = line('color','k','linestyle', 'none', 'marker','.', 'markersize', 20, ... %'erase','none', ... + 'xdata',0,'ydata',NaN); + else + h_punkty{ind+1} = line('color','k','linestyle', 'none', 'marker','.', 'markersize', 24, ... %'erase','none', ... + 'xdata',0,'ydata',NaN); + end; + punkty_x{ind+1} = NaN*punkty; punkty_y{ind+1} = NaN*punkty; + punkty_ind{ind+1} = 1; +end; + +% Grow the body +for i = 2:k+1 + for ind = 0:ile-1, + if length(x_s{ind+1}.x) > 0, + if ~isempty(punkty) + if any(punkty == i) + punkty_x{ind+1}(punkty_ind{ind+1}) = x_s{ind+1}.x(i); punkty_y{ind+1}(punkty_ind{ind+1}) = x_s{ind+1}.y(i); + punkty_ind{ind+1} = punkty_ind{ind+1} + 1; + set(h_punkty{ind+1}, 'Xdata', punkty_x{ind+1}, 'Ydata', punkty_y{ind+1}); + end + end + + j = i-1:i; + if no_head == 0, + set(head{ind+1},'xdata',x_s{ind+1}.x(i),'ydata',x_s{ind+1}.y(i)) + set(head2{ind+1},'xdata',x_s{ind+1}.x(i-1),'ydata',x_s{ind+1}.y(i-1)) + end + %set(body{ind+1},'xdata',x_s{ind+1}.x(j),'ydata',x_s{ind+1}.y(j)) + body_x{ind+1}(i) = x_s{ind+1}.x(i); body_y{ind+1}(i) = x_s{ind+1}.y(i); + set(body{ind+1},'xdata',body_x{ind+1},'ydata',body_y{ind+1}) + tail_x{ind+1}(i) = x_s{ind+1}.x(i); tail_y{ind+1}(i) = x_s{ind+1}.y(i); + set(tail{ind+1},'xdata',tail_x{ind+1},'ydata',tail_y{ind+1}) + end + end + + tic; + while toc<time + drawnow + pause(0) + end; + + if (upper(klawisz) == 'Q') | (upper(klawisz) == 27), + set(hf, 'KeyPressFcn', ''); + return; + end + if upper(klawisz) == 13, + set(gcf, 'Color', [1, 1, 0.8]); + klawisz = '~'; +% while length(klawisz) == 0, + while klawisz == '~', + drawnow + pause(0); + end + set(gcf, 'Color', 'w'); + if klawisz ~= 13, + klawisz = '~'; + end + end + if upper(klawisz) == '+', + time = time / 2; + klawisz = '~'; + end + if upper(klawisz) == '-', + time = time * 2; + klawisz = '~'; + end +end + + +% Primary loop +for i = k+2:m + for ind = 0:ile-1, + if length(x_s{ind+1}.x) > 0, + if ~isempty(punkty) + if any(punkty == i) + punkty_x{ind+1}(punkty_ind{ind+1}) = x_s{ind+1}.x(i); punkty_y{ind+1}(punkty_ind{ind+1}) = x_s{ind+1}.y(i); + punkty_ind{ind+1} = punkty_ind{ind+1} + 1; + set(h_punkty{ind+1}, 'Xdata', punkty_x{ind+1}, 'Ydata', punkty_y{ind+1}); + end + end + + j = i-1:i; + if no_head == 0, + set(head{ind+1},'xdata',x_s{ind+1}.x(i),'ydata',x_s{ind+1}.y(i)) + set(head2{ind+1},'xdata',x_s{ind+1}.x(i-1),'ydata',x_s{ind+1}.y(i-1)) + end +% set(body{ind+1},'xdata',x_s{ind+1}.x(j),'ydata',x_s{ind+1}.y(j)) +% set(tail{ind+1},'xdata',x_s{ind+1}.x(j-k),'ydata',x_s{ind+1}.y(j-k)) + tail_x{ind+1}(1:end-1) = tail_x{ind+1}(2:end); tail_y{ind+1}(1:end-1) = tail_y{ind+1}(2:end); + tail_x{ind+1}(end) = x_s{ind+1}.x(i); tail_y{ind+1}(end) = x_s{ind+1}.y(i); + set(tail{ind+1},'xdata',tail_x{ind+1},'ydata',tail_y{ind+1}) + + body_x{ind+1}(i) = x_s{ind+1}.x(i); body_y{ind+1}(i) = x_s{ind+1}.y(i); + set(body{ind+1},'xdata',body_x{ind+1},'ydata',body_y{ind+1}) + end + end + + tic; + while toc<time + drawnow + pause(0); + end + + if (upper(klawisz) == 'Q') | (upper(klawisz) == 27), + set(hf, 'KeyPressFcn', ''); + return; + end; + if upper(klawisz) == 13, + set(gcf, 'Color', [1, 1, 0.8]); + klawisz = '~'; +% while length(klawisz) == 0, + while klawisz == '~', + drawnow + pause(0); + end; + set(gcf, 'Color', 'w'); + if klawisz ~= 13, + klawisz = '~'; + end + end; + if upper(klawisz) == '+', + time = time / 2; + klawisz = '~'; + end; + if upper(klawisz) == '-', + time = time * 2; + klawisz = '~'; + end; +end; + +% Clean up the tail +for i = m+1:m+k + for ind = 0:ile-1, + if length(x_s{ind+1}.x) > 0, + j = i-1:i; + set(tail{ind+1},'xdata',x_s{ind+1}.x(j-k),'ydata',x_s{ind+1}.y(j-k)) + end + end; + + tic; + while toc<time + drawnow + pause(0); %drawnow; + end; + + if (upper(klawisz) == 'Q') | (upper(klawisz) == 27), + set(hf, 'KeyPressFcn', ''); + return; + end; + if upper(klawisz) == 13, + set(gcf, 'Color', [1, 1, 0.8]); + klawisz = '~'; +% while length(klawisz) == 0, + while klawisz == '~', + drawnow + pause(0); + end; + set(gcf, 'Color', 'w'); + if klawisz ~= 13, + klawisz = '~'; + end + end; + if upper(klawisz) == '+', + time = time / 2; + klawisz = '~'; + end; + if upper(klawisz) == '-', + time = time * 2; + klawisz = '~'; + end; + +end; + +set(hf, 'KeyPressFcn', ''); diff --git a/Ex6/matlab/design_task_1.m b/Ex6/matlab/design_task_1.m new file mode 100644 index 0000000000000000000000000000000000000000..255d1dc48de56b00fe8e0467638d7ef61bdea4d1 --- /dev/null +++ b/Ex6/matlab/design_task_1.m @@ -0,0 +1,42 @@ +function h=design_task_1 +% type = 2; +% +% %interpolation filter +% switch type, +% case 1, +% Fp1 = 22050; Fp2 = 44100; +% +% case 2, +% Fp1 = 8000; Fp2 = 32000; +% F_trans = [3000 5000]; +% end +% L = Fp2 / Fp1; + +% M = 1; +Fsymb = 1500; Fp_out = 32*Fsymb; + +L = Fp_out/Fsymb + + +% filtr dla pierwszego stopnia interpolacji +r = 0.33; +h_rc=rcos4(10*L,L, 0.33); + +K = 8192; +[H_rc, f] = freqz(h_rc/L, 1, K, L*Fsymb); + +figure(1) +subplot(3,1,1) +plot(h_rc) +subplot(3,1,2) +plot(f, abs(H_rc)) +set(gca, 'Ylim', [0, 1.1], 'Xlim', [0, L*Fsymb/2]) +subplot(3,1,3) +plot(f, 20*log10(abs(H_rc))) +set(gca, 'Ylim', [-70, 3], 'Xlim', [0, L*Fsymb/2]) + +dane.h = h_rc/L; +dane.Fp = Fsymb; +save_filter_coef('ex6_task1_h_rc', dane, 1); + + diff --git a/Ex6/matlab/ex6_task1_h_rc.coef b/Ex6/matlab/ex6_task1_h_rc.coef new file mode 100644 index 0000000000000000000000000000000000000000..6ab8d126fd59d681a95262df077eb6d7313e8a5f Binary files /dev/null and b/Ex6/matlab/ex6_task1_h_rc.coef differ diff --git a/Ex6/matlab/eyediagram2.m b/Ex6/matlab/eyediagram2.m new file mode 100644 index 0000000000000000000000000000000000000000..0ced67e5d8f51e6ebaeacb1199538a5f66c0042c --- /dev/null +++ b/Ex6/matlab/eyediagram2.m @@ -0,0 +1,69 @@ +function hf = eyediagram2(x, offset, Nsymb, hf) + +if nargin == 4, + figure(hf); +else + hf = figure; +end +subplot(1,1,1) +if nargin == 2, + Nsymb = offset; + offset = ceil((Nsymb-1)/2); +% Nsymb = length(x)+1; +end +x(ceil(length(x)/Nsymb)*Nsymb) = 0; +hp = []; +% t_ = linspace(-Nsymb/2, Nsymb/2, Nsymb); +t_ = 0:Nsymb; +ind_ = 1:(Nsymb+1); +% if rem(Nsymb,2) == 1, +% t_ = (0:Nsymb+1) - Nsymb/2 - 0.5; +% +% ind_ = [0:Nsymb+1]; +% else +% t_ = (-1:Nsymb) - Nsymb/2 + 0.5; +% +% ind_ = [-1:Nsymb]; +% end + + +dt = offset - round(offset); +t_ = t_ + dt; offset = round(offset); + +% t_ = t_ / Nsymb; + +if sum(abs(imag(x))) == 0, + subplot(1,1,1); + plot(t_, t_*0, 'k:'); + hold on + plot([0, 0], [-1.1, 1.1], 'k:'); + % plot(t_, +1+t_*0, 'k:'); + % plot(t_, -1+t_*0, 'k:'); + for ind = offset:Nsymb:length(x)-(Nsymb+1), + x_ = x(ind+ind_); + + hp(end+1) = plot(t_, real(x_)); +% hp(end+1) = plot(t_, imag(x_), 'r'); + end + hold off +else + subplot(1,2,1); + plot(t_, t_*0, 'k:'); + hold on + plot([0, 0], [-1.1, 1.1], 'k:'); + % plot(t_, +1+t_*0, 'k:'); + % plot(t_, -1+t_*0, 'k:'); + subplot(1,2,2); + plot(t_, t_*0, 'k:'); + hold on + plot([0, 0], [-1.1, 1.1], 'k:'); + for ind = offset:Nsymb:length(x)-(Nsymb+1), + x_ = x(ind+ind_); + + subplot(1,2,1); + hp(end+1) = plot(t_, real(x_)); + subplot(1,2,2); + hp(end+1) = plot(t_, imag(x_), 'r'); + end + hold off +end diff --git a/Ex6/matlab/load_filter_coef.m b/Ex6/matlab/load_filter_coef.m new file mode 100644 index 0000000000000000000000000000000000000000..8c8574c248dfce0cab89673b49921682f9957180 --- /dev/null +++ b/Ex6/matlab/load_filter_coef.m @@ -0,0 +1,107 @@ +function [coefficients, file_version] = load_filter_coef(filename) +% [coefficients, file_version] = load_filter_coef(filename) +% +% FIR filter coefficients +% coefficients.h +% - can be cell vector to store multiple FIR impulse responces +% For file_version >= 1 (default == 0), +% coefficients.Fp - sampling frequency +% +% This file is a part of Digital Signal Processing Engine +% \author Marek Blok +% \date 2008.03.28 + +% /*! File format (*.coef) - this is open format, for general use +% * (not only for storing coefficients) +% * +ind = find(filename == '.'); +if length(ind) == 0, + filename = [filename, '.coef']; +end +plik = fopen(filename, 'rb'); + +% * - (uchar) 1B - file version number +file_version = fread(plik, 1, 'uchar') + +switch file_version, + case 0, + case 1, + % * - (uint) 4B - Sampling frequency + coefficients.Fp = fread(plik, 1, 'uint32') + otherwise, + fclose(plik); + error('This version of coefficients file is unsupported'); +end + +% * - data - coefficients data (depends on fle version) +% * . +% * Data segment format: +% * -# (version: 0x00) +% * - (uchar) 1B - number of sample dimensions +% * 1 - real, 2 - complex, ... +no_of_channels = fread(plik, 1, 'uchar'); % complex +if no_of_channels == 1, + isComplex = 0; +elseif no_of_channels == 2, + isComplex = 1; +else + fclose(plik); + error('to many channels'); +end +% * - (uchar) 1B - sample component type +% * - DSP::e::FileType::FT_float (=1) : C++ float (32bit floating point) +% * - DSP::e::FileType::FT_short (=2) : C++ short (16bit signed integer) +% * - DSP::e::FileType::FT_uchar (=3) : C++ unsigned char (8bit unsigned integer with bias (0x80)) +% * - DSP::e::FileType::FT_double (=7) : C++ double (64bit floating point) +% * - DSP::e::FileType::FT_long_double (=8) : C++ long double (80bit floating point) +sample_type = fread(plik, 1, 'uchar'); +switch sample_type, + case 1, + sample_type = 'float'; + case 2, + sample_type = 'short'; + case 3, + sample_type = 'uchar'; + case 7, + sample_type = 'double'; + case 8, + sample_type = 'float80'; + otherwise + fclose(plik); + error('unknown sample type'); +end + +% * - (uchar) 1B - number of vectors +% * - 1 - FIR filter coefficients (one vector) +% * - 2 - IIR filter coefficients (two vectors) +% * - (x number of vectors) +% * - (ushort) 2B - number of samples in vector +% * - (x number of samples) +% * - (x number of sample dimensions) +% * - (sample componet type) xB - sample component +% * e.g. real, imag part +resp_no = fread(plik, 1, 'uchar'); + +if resp_no == 1, + N_FIR = fread(plik, 1, 'uint16'); + if isComplex, + dane = fread(plik, 2*N_FIR, sample_type); + coefficients.h = dane(1:2:2*N_FIR) + j*dane(2:2:2*N_FIR); + else + dane = fread(plik, N_FIR, sample_type); + coefficients.h = dane; + end +else + for ind_resp = 1:resp_no, + N_FIR = fread(plik, 1, 'uint16'); + if isComplex, + dane = fread(plik, 2*N_FIR, sample_type); + coefficients.h{ind_resp} = dane(1:2:2*N_FIR) + j*dane(2:2:2*N_FIR); + else + dane = fread(plik, N_FIR, sample_type); + coefficients.h{ind_resp} = dane; + end + end +end + +fclose(plik); diff --git a/Ex6/matlab/rcos4.m b/Ex6/matlab/rcos4.m new file mode 100644 index 0000000000000000000000000000000000000000..233f780cb878f5515966b551eeb2edbad8a56cda --- /dev/null +++ b/Ex6/matlab/rcos4.m @@ -0,0 +1,51 @@ +function h_rc=rcos4(N,L, r) +%square root raised cosine + +fp=1000; + +if nargin==2, + r=0.5; +end + +n=(0:N-1)-(N-1)/2; +% t=n/fp; T=L/fp; + +% % L=pi*L; h_rc=sinc(pi*n/L).*cos(pi*r*n/L)./(L-4*(r.^2)*(n.^2)/L); +% h_rc=sinc(t/T).*cos(r*pi*t/T)./(1-4*(r.^2)*(t.^2)/T.^2); +h_rc=(1-r)*sinc((1-r)*n/L) + ... + r*( sinc(r*n/L+1/4).*cos(pi*(n/L+1/4)) + ... + sinc(r*n/L-1/4).*cos(pi*(n/L-1/4)) ); +% % h_rc= 4*r* ( cos((1+r)*pi*n/L) + sin((1-r)*pi*n/L)./(4*r*n/L)) ... +% % ./ (pi*sqrt(1/L)*(1-(4*r*n/L).^2)); +% +ind=~isfinite(h_rc); +h_rc(ind)=0; + +if nargout==0, + figure(1) + subplot(3,1,1) + stem(n/L,h_rc) + subplot(3,1,2) + n=-(N-1):(N-1); +% size(n) +% size(conv(h_rc,h_rc)) + stem(n/L,conv(h_rc,h_rc)) + subplot(3,1,3) + H=freqz(h_rc,1,2048, 'whole'); + H=abs(H(1:end/2)); + f=linspace(0,1000,length(H)); + plot(f,H) + + H=freqz(ones(1,L),1,2048, 'whole'); + H=abs(H(1:end/2)); + f=linspace(0,1000,length(H)); + hold on + plot(f,H,'k') + hold off + H=freqz(conv(h_rc,h_rc)/L,1,2048, 'whole'); + H=abs(H(1:end/2)); + f=linspace(0,1000,length(H)); + hold on + plot(f,H,'m') + hold off +end \ No newline at end of file diff --git a/Ex6/matlab/readaudiofile.m b/Ex6/matlab/readaudiofile.m new file mode 100644 index 0000000000000000000000000000000000000000..825a9a890d520cb59e3dda822abed693cc2fc262 --- /dev/null +++ b/Ex6/matlab/readaudiofile.m @@ -0,0 +1,215 @@ +function [x, Fs] = readaudiofile(filename, param) +% [x, Fs] = readaudiofile(filename, param) +% +% returns vector x of the size SIZE=[samples channels]. +% eg. x = x(:,1) + j*x(:,2); +% +% special uses: +% param == 'size': returns SIZE in x +% param == 'cplx': for stereo files returns x as a complex vector instead of matrix +% +% supported file types: +% *.flt +% *.wav +% *.tape +% last modification: 2021.03.29 +% Author: Marek Blok + +return_cplx = 0; +if nargin == 1, + param = inf; +else + if strcmp(param, 'cplx') == 1, + return_cplx = 1; + param = inf; + end +end + +ind = find(filename == '.'); +if length(ind) == 0, + file_type = 'w'; % *.wav +else + ind = ind(end); + + temp = filename(ind+1:end); + + file_type = 'u'; % unknown format + if strcmp(temp, 'flt') == 1, + file_type = 'f'; % two channel floting point + elseif strcmp(temp, 'wav') == 1, + file_type = 'w'; % *.wav + elseif strcmp(temp, 'tape') == 1, + file_type = 't'; % *.tape + end +end + +switch file_type, + case 'w', + if strcmp(param, 'size') == 1, + if exist('audioread','file') == 0 + x = wavread(filename, 'size'); + % siz = [samples channels]. + else + info = audioinfo(filename); + x = [info.TotalSamples, info.NumChannels]; + Fs = info.SampleRate; + end + else + if isfinite(param) + if exist('audioread','file') == 0 + [x, Fs] = wavread(filename, param); + else + if length(param) == 1 + param = [1, param]; + end + [x, Fs] = audioread(filename, param); + end + else + if exist('audioread','file') == 0 + [x, Fs] = wavread(filename); + else + [x, Fs] = audioread(filename); + end + end + end + + case 't' + plik = fopen(filename, 'rb'); + if plik == -1, + error('File does not exist !!!'); + end + + header.size = fread(plik, 1, 'uint32', 0) + 4; + + header.fname = char(fread(plik, 256, 'char', 0).'); + header.cfg_fname = char(fread(plik, 256, 'char', 0).'); + header.sw_rev = fread(plik, 1, 'uint32', 0); + header.hw_rev = fread(plik, 1, 'uint32', 0); + header.file_ = fread(plik, 1, 'uint32', 0); + header.tape_type = fread(plik, 1, 'uint32', 0); + header.start_time = fread(plik, 1, 'int32', 0); % time_t + header.end_time = fread(plik, 1, 'int32', 0); % time_t + + header.total_samples = fread(plik, 1, 'uint32', 0); + file_length = header.total_samples * 4 + header.size + header.current_sample = fread(plik, 1, 'uint32', 0); + header.loop_start = fread(plik, 1, 'int64', 0); + header.loop_end = fread(plik, 1, 'int64', 0); + header.loop = fread(plik, 1, 'int32', 0); + header.group_size_32 = fread(plik, 1, 'uint32', 0); + header.block_size = fread(plik, 1, 'uint32', 0); + header.block_count = fread(plik, 1, 'uint32', 0); + header.fifo_size = fread(plik, 1, 'uint32', 0); + + + header.comment = char(fread(plik, 256, 'char', 0).'); + header.tmp = char(fread(plik, 20, 'char', 0).'); % time_t + header.status = fread(plik, 1, 'uint32', 0); + header.timestamps = fread(plik, 1, 'int32', 0); + header.freq = fread(plik, 1, 'float', 0); + header.cplx_datarate = fread(plik, 1, 'float', 0); + +% ftell(plik) + header.reserved = fread(plik, 128, 'uint32', 0); +% header.reserved.' + + header + ftell(plik) + + header.sample_type = 2; + header.ch_no = 2; + header.Fs = NaN; + + sample_type = 'int16'; + sample_size = 2; + + header_size = header.size; + if strcmp(param, 'size') == 1, + fseek(plik, 0, 'eof'); + size = (ftell(plik) - header_size) / sample_size / header.ch_no; % sizeof(float) *2 + x = size; + else + fseek(plik, header_size, 'bof'); + + len = param(1); + if length(param) > 1, + fseek(plik, sample_size*header.ch_no*(param(1)-1), 'cof'); + len = param(2) - param(1) + 1; + end + +% x = fread(plik, [header.ch_no, len], sample_type); + x = fread(plik, [header.ch_no, len], sample_type, 0); + x = x.'; + end + fclose(plik); + + case 'f' + plik = fopen(filename, 'rb'); + if plik == -1, + error('File does not exist !!!'); + end + + % 3 B - wersja pliku (w tym typ próbek) + % 1 B - liczba kanałów + % 4 B - szybkoć próbkowania + header_size = 8; + header.ver = fread(plik, 1, 'uint8'); + header.sample_type = fread(plik, 1, 'uint16'); + header.ch_no = fread(plik, 1, 'uint8'); + header.Fs = fread(plik, 1, 'uint32'); + + Fs = header.Fs; + + switch (header.ver), + case 0, + switch (header.sample_type), + case 0, + sample_type = 'float'; + sample_size = 4; + case 1, + sample_type = 'uchar'; + sample_size = 1; + case 2, + sample_type = 'short'; + sample_size = 2; + case 3, + sample_type = 'int'; + sample_size = 4; + otherwise + error('Unsupported *.flt sample type !!!'); + end + otherwise + error('Unsupported *.flt file version !!!'); + end + + + if strcmp(param, 'size') == 1, + fseek(plik, 0, 'eof'); + size = (ftell(plik) - header_size) / sample_size / header.ch_no; % sizeof(float) *2 + x = size; + else + len = param(1); + status = 0; + if length(param) > 1, + status = fseek(plik, sample_size*header.ch_no*(param(1)-1), 'cof'); + len = param(2) - param(1) + 1; + end + + if (status == -1) + x = []; + else + x = fread(plik, [header.ch_no, len], sample_type); + x = x.'; + end + end + + fclose(plik); + otherwise + error('Unsupported file format !!!'); +end + +if return_cplx == 1, + if length(x(1,:)) == 2, + x = x(:,1) + j*x(:,2); + end +end \ No newline at end of file diff --git a/Ex6/matlab/save_filter_coef.m b/Ex6/matlab/save_filter_coef.m new file mode 100644 index 0000000000000000000000000000000000000000..5a1bf6b5e73b6351236dce20deef83096b96eb4c --- /dev/null +++ b/Ex6/matlab/save_filter_coef.m @@ -0,0 +1,226 @@ +function save_filter_coef(filename, coefficients, file_version) +% save_filter_coef(filename, coefficients [, file_version]) +% +% FIR filter coefficients +% coefficients.h +% - can be cell vector to store multiple FIR impulse responces +% - if any of the impulse responses is complex then all responses will +% be stored as complex +% IIR filter coefficients +% coefficients.a +% coefficients.b +% +% For file_version >= 1 (default == 0), +% coefficients.Fp - sampling frequency +% +% This file is a part of Digital Signal Processing Engine +% +% File format (*.coef) - this is open format, for general use +% * (not only for storing coefficients) +% +% File format (*.h) - this is C++ header file for DSPElib with hardcoded *.coef equivalent +% +% \author Marek Blok +% \date 2020.02.22 + +if nargin == 2, + file_version = 0; +end + +ind = find(filename == '.'); +if length(ind) == 0, + filename = [filename, '.coef']; +end + +% detect mode based on file extention +[PATHSTR,NAME,EXT] = fileparts(filename); +switch EXT + case '.h' + mode = 'h-file'; + plik = fopen(filename, 'wt'); + + case '.coef' + mode = 'coefs-file'; + plik = fopen(filename, 'wb'); + + otherwise + mode = 'unknown-file'; + error(mode); +end + +switch mode + case 'h-file' + fprintf(plik, '// save_filter_coef output (hard coded coefficients)\n'); + fprintf(plik, '#pragma once\n'); + fprintf(plik, '#include <DSP_lib.h>\n'); + fprintf(plik, '\n'); + + if isfield(coefficients, 'h'), + h = coefficients.h; + + for ind_h = 0:length(h)-1 + fprintf(plik, 'DSP::Float_vector Farrow_coefs_row_%i = {', ind_h); + h_tmp = h{ind_h+1}; + for ind_p = 0:length(h_tmp)-1, + if ind_p > 0 + fprintf(plik, ', '); + end + fprintf(plik, '%.15ff', h_tmp(ind_p+1)); + end + %0.1f, 0.2f, 0.1f + fprintf(plik, '};\n'); + end + fprintf(plik, '\n'); + + %vector <DSP::Float_vector> Farrow_coefs = { coefs_0, coefs_1, coefs_2 }; + fprintf(plik, 'vector <DSP::Float_vector> Farrow_coefs = {'); + for ind_h = 0:length(h)-1 + if ind_h > 0 + fprintf(plik, ', '); + end + if (ind_h < length(h_tmp)) & (rem(ind_h, 5) == 0) + fprintf(plik, '\n '); + end + fprintf(plik, 'Farrow_coefs_row_%i', ind_h); + end + fprintf(plik, '\n };\n'); + + fprintf(plik, '\n'); + fprintf(plik, 'const unsigned int p_Farrow = %i; // should be equal to Farrow_coefs[0].size()-1\n', length(h{1})-1); + fprintf(plik, 'const unsigned int N_FSD = %i; // should be equal to Farrow_coefs.size()\n', length(h)); + end + + if isfield(coefficients, 'b'), + fclose all; + error('unsupported: coefficients.b'); + end + if isfield(coefficients, 'a'), + fclose all; + error('unsupported: coefficients.a'); + end + + case 'coefs-file' + % * - (uchar) 1B - file version number + fwrite(plik, file_version, 'uchar'); + + switch file_version, + case 0, + case 1, + % * - (uint) 4B - Sampling frequency + if isfield(coefficients, 'Fp'), + fwrite(plik, coefficients.Fp, 'uint32'); + else + fclose(plik); + error('Input data does not contain Fp'); + end + otherwise, + fclose(plik); + error('This version of coefficients file is unsupported'); + end + + if isfield(coefficients, 'h'), + isFIR = 1; + if iscell(coefficients.h) + resp_no = length(coefficients.h); + % if resp_no = 1; + % coefficients.h = coefficients.h{1}; + % end + else + resp_no = 1; + end + if resp_no == 1, + isComplex = any(imag(coefficients.h(:))); + else + isComplex = false; + for ind_resp = 1:resp_no, + isComplex = isComplex | any(imag(coefficients.h{ind_resp}(:))); + end + end + else + isFIR = 0; + isComplex = any(imag(coefficients.a(:))) | any(imag(coefficients.b(:))); + end + + % * - data - coefficients data (depends on fle version) + % * . + % * Data segment format: + % * -# (version: 0x00) + % * - (uchar) 1B - number of sample dimensions + % * 1 - real, 2 - complex, ... + if isComplex, + fwrite(plik, 2, 'uchar'); % complex + else + fwrite(plik, 1, 'uchar'); % real + end + % * - (uchar) 1B - sample component type + % * - DSP::e::FileType::FT_float (=1) : C++ float (32bit floating point) + % * - DSP::e::FileType::FT_short (=2) : C++ short (16bit signed integer) + % * - DSP::e::FileType::FT_uchar (=3) : C++ unsigned char (8bit unsigned integer with bias (0x80)) + % * - DSP::e::FileType::FT_double (=7) : C++ double (64bit floating point) + % * - DSP::e::FileType::FT_long_double (=8) : C++ long double (80bit floating point) + fwrite(plik, 1, 'uchar'); + + % * - (uchar) 1B - number of vectors + % * - 1 - FIR filter coefficients (one vector) + % * - 2 - IIR filter coefficients (two vectors) + % * - (x number of vectors) + % * - (ushort) 2B - number of samples in vector + % * - (x number of samples) + % * - (x number of sample dimensions) + % * - (sample componet type) xB - sample component + % * e.g. real, imag part + if isFIR, + fwrite(plik, resp_no, 'uchar'); + + if iscell(coefficients.h) + for ind_resp = 1:resp_no, + N_FIR = length(coefficients.h{ind_resp}); + fwrite(plik, N_FIR, 'uint16'); + if isComplex, + dane(1:2:2*N_FIR) = real(coefficients.h{ind_resp}); + dane(2:2:2*N_FIR) = imag(coefficients.h{ind_resp}); + fwrite(plik, dane, 'float'); + else + fwrite(plik, real(coefficients.h{ind_resp}), 'float'); + end + end + else + N_FIR = length(coefficients.h); + fwrite(plik, N_FIR, 'uint16'); + if isComplex, + dane(1:2:2*N_FIR) = real(coefficients.h); + dane(2:2:2*N_FIR) = imag(coefficients.h); + fwrite(plik, dane, 'float'); + else + fwrite(plik, real(coefficients.h), 'float'); + end + end + + else + fwrite(plik, 2, 'uchar'); + + N_a = length(coefficients.a); + fwrite(plik, N_a, 'uint16'); + if isComplex, + dane(1:2:2*N_a) = real(coefficients.a); + dane(2:2:2*N_a) = imag(coefficients.a); + fwrite(plik, dane, 'float'); + else + fwrite(plik, real(coefficients.a), 'float'); + end + + + N_b = length(coefficients.b); + fwrite(plik, N_b, 'uint16'); + if isComplex, + dane(1:2:2*N_b) = real(coefficients.b); + dane(2:2:2*N_b) = imag(coefficients.b); + fwrite(plik, dane, 'float'); + else + fwrite(plik, real(coefficients.b), 'float'); + end + end + +end +fclose(plik); + diff --git a/Ex6/matlab/test.m b/Ex6/matlab/test.m new file mode 100644 index 0000000000000000000000000000000000000000..341dc4f03a1bfe7a3b19720fd31b137616cef368 --- /dev/null +++ b/Ex6/matlab/test.m @@ -0,0 +1,57 @@ +function test + +Fsymb = 3000; +K = 32; +[x, Fp] = readaudiofile('../ex6_task1.flt', 'cplx'); +[xb, Fp] = readaudiofile('../ex6_task1b.flt', 'cplx'); + +length(x) +Fp + +% y = []; +% while length(xb) >= K +% y = [y; xb(K:-1:1)]; +% xb(1:K) = []; +% end +% xb = [y; xb]; + +figure(1) +subplot(2,1,1) +plot(real(x)) +hold on +plot(real(xb), 'r') +hold off +subplot(2,1,2) +plot(imag(x)) + +K_psd = 8*1024; + +figure(2) +specgram(x) + +figure(3) +% psd(x) +F = linspace(-Fp/2, Fp/2, 2*K_psd+1); F(end) = []; +% pwelch(x, blackman(K_psd), ceil(0.5*K_psd), F, Fp); +[Pxx, F] = pwelch(x, blackman(K_psd), ceil(0.5*K_psd), F, Fp); +[Pxx_b, F] = pwelch(xb, blackman(K_psd), ceil(0.5*K_psd), F, Fp); +Pxx_dB = 10*log10(Pxx); +Pxx_b_dB = 10*log10(Pxx_b); +Pxx_dB_max = ceil(max(Pxx_dB)/3)*3; Y_range = [-100, 0]+Pxx_dB_max; +plot([0,0], Y_range, 'r--'); +set(gca, 'Xlim', [-Fp/2, Fp/2]/1000, 'Ylim', Y_range); +hold on +for ind = 1:ceil(K/2), + plot(ind*Fsymb/1000*[1,1], Y_range, 'r--'); + plot(-ind*Fsymb/1000*[1,1], Y_range, 'r--'); + + plot((ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); + plot(-(ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); +end +plot(F/1000,Pxx_dB); +plot(F/1000,Pxx_b_dB, 'm:'); +hold off +% set(gca,'Xtick', (-(floor(K/2)+1/2):1/2:(ceil(K/2)+1/2))*Fsymb/1000, 'Xgrid', 'on'); + +xlabel('Frequency [kHz]') +ylabel('Power spectral density [dB/Hz]') \ No newline at end of file diff --git a/Ex6/matlab/test_DBPSK.m b/Ex6/matlab/test_DBPSK.m new file mode 100644 index 0000000000000000000000000000000000000000..cfbdbd05152232b533657eefe44784ea5c683558 --- /dev/null +++ b/Ex6/matlab/test_DBPSK.m @@ -0,0 +1,32 @@ +function test_DBPSK + +x = readaudiofile('../ex6_task2_text_in.flt'); +x_en = readaudiofile('../ex6_task2_text_en.flt', 'cplx'); +x_de = readaudiofile('../ex6_task2_text_de.flt'); + +figure(1) +subplot(3,1,1) +stem(x(1:20)) +subplot(3,1,2) +stem(real(x_en(1:20))) +hold on +stem(imag(x_en(1:20)), 'r') +hold off + +subplot(3,1,3) +stem(2*(x_de(1:20)-0.5)) + +return +y(1) = 1; +for ind = 1:length(x_en), + y(ind+1) = y(1)*x_en(ind); +end + +x_en2 = [1; x_en]; +x_en2d = [x_en; 1]; + +y = x_en2 .* conj(x_en2d); + +hold on +stem(real(y(1:20))*0.9, 'r') +hold off diff --git a/Ex6/matlab/test_FMD_signal.m b/Ex6/matlab/test_FMD_signal.m new file mode 100644 index 0000000000000000000000000000000000000000..883e63e15df586ec5eeb81890588f10b70996c40 --- /dev/null +++ b/Ex6/matlab/test_FMD_signal.m @@ -0,0 +1,72 @@ +function test_FMD_signal(filename, N_DFT, Fsymb, use_only_real_part) + +if nargin == 0, + % parametry sygnału wielotonowego + Fsymb = 3000; + N_DFT = 16; + filename = '../ex6_task1.flt'; + use_only_real_part = 1; +end + +try + [x, Fp] = readaudiofile(filename, 'cplx'); +catch + [x, Fp] = readaudiofile(filename); +end + +K_psd = 8*1024; +K_spect = 1024; + +if use_only_real_part ~= 0, + x = real(x); + % freq_range = 'onesided'; + if rem(K_spect,2) == 1, + K_spect = K_spect+1; + end + F = linspace(0, Fp/2, K_spect+1); F(end) = []; +else + % freq_range = 'centered'; + F = linspace(-Fp/2, Fp/2, 2*K_spect+1); F(end) = []; +end + +length(x) +Fp + +figure(1) +subplot(2,1,1) +plot(real(x)) +subplot(2,1,2) +plot(imag(x)) + +F_psd = linspace(-Fp/2, Fp/2, 2*K_psd+1); F_psd(end) = []; + +figure(12) +set(12, 'Name', 'Spectrogram analizowanego sygnału'); +% specgram(x) +spectrogram(x, blackman(K_spect), ceil(0.5*K_spect), F, Fp, 'yaxis'); +colormap(jet) + +figure(13) +set(13, 'Name', 'Widmo gęstoci mocy analizowanego sygnału'); +% psd(x) +% pwelch(x, blackman(K_psd), ceil(0.5*K_psd), F, Fp); +Pxx = pwelch(x, blackman(K_psd), ceil(0.5*K_psd), F_psd, Fp); +Pxx_dB = 10*log10(Pxx); +Pxx_dB_max = ceil(max(Pxx_dB)/3)*3; Y_range = [-100, 0]+Pxx_dB_max; +plot([0,0], Y_range, 'r--'); +set(gca, 'Xlim', [-Fp/2, Fp/2]/1000, 'Ylim', Y_range); +hold on +for ind = 1:ceil(N_DFT/2), + plot(ind*Fsymb/1000*[1,1], Y_range, 'r--'); + plot(-ind*Fsymb/1000*[1,1], Y_range, 'r--'); + + plot((ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); + plot(-(ind-0.5)*Fsymb/1000*[1,1], Y_range, 'k-'); +end +plot(F_psd/1000,Pxx_dB); +hold off +% set(gca,'Xtick', (-(floor(K/2)+1/2):1/2:(ceil(K/2)+1/2))*Fsymb/1000, 'Xgrid', 'on'); + +xlabel('Frequency [kHz]') +ylabel('Power spectral density [dB/Hz]') + diff --git a/Ex6/matlab/test_task2.m b/Ex6/matlab/test_task2.m new file mode 100644 index 0000000000000000000000000000000000000000..150ebf53f28f2bc5b41fbcd0ba6260f0729e02ff --- /dev/null +++ b/Ex6/matlab/test_task2.m @@ -0,0 +1,116 @@ +function test_task2(ind) +if nargin == 0, + ind = 2*(3+2*4); +end +[x, Fp] = readaudiofile('../ex6_task1.flt'); +[y, Fp] = readaudiofile('../ex6_task2a.flt', 'cplx'); +[y_ref, Fp] = readaudiofile('../ex6_task1a.flt', 'cplx'); + +K = 32; M = 8; +% K = 16; M = 8; + +figure(1) +subplot(2,1,1) +plot(x) +subplot(2,2,3) +plot(y, 'b.') +axis equal +subplot(2,2,4) +plot(y_ref, 'b.') +axis equal +% % plot(y) + +% y = y(21+1:end); + +figure(2) +subplot(2,1,1) +stem(real(y)/max(abs(y)), 'b') +hold on +stem(real(y_ref), 'r') +hold off +subplot(2,1,2) +[c, l] = xcorr(real(y), real(y_ref)); +plot(l, c) + +figure(3) +subplot(2,1,1) +stem(imag(y)/max(abs(y)), 'b') +hold on +stem(imag(y_ref), 'r') +hold off +subplot(2,1,2) +[c, l] = xcorr(imag(y), imag(y_ref)); +plot(l, c) + +n = [0:length(y)-1].'; +het = exp(-j*(8)*pi/(32)*n); +% het = 1; +figure(13) +psd(het.*y) +% return + +% xt{1} = ((-1).^(1:length(y)).').*y/max(abs(y)); +xt{1} = het.*y/max(abs(y)); +xt{2} = y_ref; +trajekt_(xt, 10, 1:M:length(y), 0, 1) +tmp = xt{1}; +eyediagram2(tmp(1:100*M), 0, M, 15); +pause + +y = y(1:M:end); +% y = y.*exp(j*pi/4); +s_re = real(y) > 0; +s_im = imag(y) > 0; + +bin = zeros(1,2*length(s_re)); +bin(1:2:end) = s_re; +bin(2:2:end) = s_im; + +bits_per_char = 8; +%ind = 0; + +h = fopen('../Ex6_task1.cpp'); +bin2 = fread(h, 800, 'ubit1').'; +fclose(h) +% +figure(4) +subplot(2,1,1) +[c, l] = xcorr(s_re, bin2(1:2:end)); +plot(l, c) +subplot(2,1,2) +[c, l] = xcorr(s_re, bin2(2:2:end)); +plot(l, c) + +% return +figure(5) +subplot(2,1,1) +stem(bin, 'b') +hold on +stem(bin2, 'r') +hold off +subplot(2,1,2) +[c, l] = xcorr(bin, bin2); +plot(l, c) + +% bin = bin_root.' +bin = bin((ind+1):end); +% for ind= 1:bits_per_char, +for ind= 1, + bin_ = bin(ind:end); + B = floor(length(bin_)/bits_per_char)*bits_per_char; + + tekst = bin_(1:bits_per_char:B); + for bit = 1:bits_per_char-1, +% tekst = tekst + (2^bit)*bin_((bit+1):bits_per_char:B); + tekst = tekst + (2^((bits_per_char-1)-bit))*bin_((bit+1):bits_per_char:B); + end + char(tekst) +end + + +% +% B_pre = length(pre)*bits_per_char; +% bin_pre = zeros(B_pre,1); +% for ind = 0:bits_per_char-1, +% bin_pre((ind+1):bits_per_char:end) = rem(floor(pre / (2^ind)),2); +% end diff --git a/Ex6/matlab/trajekt_.m b/Ex6/matlab/trajekt_.m new file mode 100644 index 0000000000000000000000000000000000000000..696ad413ada22898d471568f86e4b380856865ea --- /dev/null +++ b/Ex6/matlab/trajekt_.m @@ -0,0 +1,403 @@ +function klawisz=trajekt_(x_s, time, punkty, no_head, pause_on_start) +% \added 2019.10.18 żółte tło okna w trybie pauzy +% \added 2019.10.18 prosty tryb testowania (ustawiajšc test_mode = 1; w linii 11) +% \fixed 2019.10.18 wyłšczenie zoom przed definiowaniem 'KeyPressFcn' +% \added 2017.10.24 dodano obsługę trybu, w którym x_s zawiera dane i +% etykiety (interpreter LaTeX): x_s{1}.data = ..., x_s{1}.label = '?'; +% \fixed 2015.10.27 addapted to matlab 2015b +% \fixed 2006.11.14 fixed axes position in single and double plot mode +% \added 2006.11.14 input option no_head +% \added 2006.11.14 input option pause_on_start + +test_mode = 0; +if (test_mode == 1) & (nargin == 0) + x_s = exp(sqrt(-1)*0.1*[0:1000]); + klawisz = trajekt_(x_s, 100, [], 0, 1); + return; +end + +if nargin < 5, + pause_on_start = 0; +end +if nargin < 4, + no_head = 1; +end +if nargin < 3, + punkty = []; +end +labels = {}; + + +if nargin == 0, +error('Not enough input arguments.'); +else + if isreal(x_s), + ile = 1; + x = x_s; + y = x; x = 1:length(y); + + clear x_s; + x_s{1}.x = x; + x_s{1}.y = y; + max1=max(abs(x(~isinf(x)))); + zakres=[-max1 max1 -max1 max1]; + else + if iscell(x_s) == 0, + ile = 1; + y=imag(x_s); + x=real(x_s); + clear x_s; + x_s{1}.x = x; + x_s{1}.y = y; + max1=max(max(abs(x(~isinf(x)))),max(abs(y(~isinf(y))))); + zakres=[-max1 max1 -max1 max1]; + else + x_s_tmp = x_s; + clear x_s + + ile = length(x_s_tmp); + max1 = eps; + for ind = 1:ile, + if isfield(x_s_tmp{ind}, 'label'), + labels{ind} = x_s_tmp{ind}.label; + end + if isfield(x_s_tmp{ind}, 'data'), + x_s_tmp{ind} = x_s_tmp{ind}.data; + end + x=real(x_s_tmp{ind}); + y=imag(x_s_tmp{ind}); + x_s{ind}.x = x; + x_s{ind}.y = y; + + max1=max([max(abs(x(~isinf(x)))),max(abs(y(~isinf(y)))), max1]); + end + zakres=[-max1 max1 -max1 max1]; + end + end +end + + +% if nargin == 0, +% error('Not enough input arguments.'); +% else +% if isreal(x), +% % y = x; x = 1:length(y); +% % max_y=max(abs(x(~isinf(x)))); +% % min_y=-max_x; +% % max_x=length(y); +% % min_x=1; +% y = x; x = 1:length(y); +% max_y=10; +% min_y=-10; +% max_x=10; +% min_x=10; +% else +% % y=imag(x); +% % x=real(x); +% % max_x=max(abs(x(~isinf(x)))); +% % min_x=-max_x; +% % max_y=max_x; +% % min_y=-max_x; +% y=imag(x); +% x=real(x); +% max_x=10; +% min_x=-10; +% max_y=10; +% min_y=-10; +% +% end +% end +if nargin==1, + time=0.01; +else + time=time/length(x); +end +p = 0.02; + +hf=findobj(0,'tag','trajektoria'); +if isempty(hf) + hf=figure('NextPlot','add', 'Color', 'w'); +else + figure(hf); +end +set(hf,'tag','trajektoria', 'Name', 'Trajektorie - dr inż. Marek Blok (14.11.2006)'); +delete(findobj(hf,'type', 'axes')); + +global klawisz +global trajekt_hf + +if pause_on_start == 0, + klawisz = '~'; % od razu ruszaj +else + klawisz = 13; % pauza na starcie +end +trajekt_hf = hf; +% set(hf, 'KeyPressFcn', 'global klawisz; fig = get(0, ''PointerWindow''); if fig ~= 0, klawisz = get(fig,''CurrentCharacter''); if isempty(klawisz), klawisz = ''~''; end; end;'); +zoom(hf, 'off'); +set(hf, 'KeyPressFcn', 'global klawisz; global trajekt_hf, fig = trajekt_hf; if fig ~= 0, klawisz = get(fig,''CurrentCharacter''); if isempty(klawisz), klawisz = ''~''; end; end;'); + +min_x=-5; +max_x=5; +min_y=-5; +max_y=5; + +if ile == 1, + ax{1}=axes('Units','Normalized','Position',[0, 0, 1, 1]+ [1, 1, -2, -2]*0.05); + if ~ishold, + axis([min_x max_x ... + min_y max_y]/5) + axis square + end + if length(labels) >= 1 + title(labels{1}, 'Interpreter', 'latex'); + end + set(ax{1},'Xtick', -1:0.5:1, 'Ytick', -1:0.5:1, 'Xgrid','on','Ygrid','on'); + co = get(ax{1},'colororder'); +elseif ile == 2, + for ind = 0:1, + ax{ind+1}=axes('Units','Normalized','Position',[ind*0.5 0 [0.5 1]]+ [1, 1, -2, -2]*0.05); + if ~ishold, + axis([min_x max_x ... + min_y max_y]/5) + axis square + end + if length(labels) >= 2 + title(labels{ind+1}, 'Interpreter', 'latex'); + end + set(ax{ind+1},'Xtick', -1:0.5:1, 'Ytick', -1:0.5:1, 'Xgrid','on','Ygrid','on'); + end + co = get(ax{1},'colororder'); +else + % 12 + for ind = 0:ile-1, + % 4 x 3 + x_ = rem(ind,4); + y_ = (ind-x_)/4; + ax{ind+1}=axes('Units','Normalized','Position',[x_*0.25 1-(y_+1)*0.33 [0.25 0.33]*0.95]); + if ~ishold, + axis([min_x max_x ... + min_y max_y]/5) + axis square + end + if length(labels) >= ile + title(labels{ind+1}, 'Interpreter', 'latex'); + end + set(ax{ind+1},'Xtick', -1:0.5:1, 'Ytick', -1:0.5:1, 'Xgrid','on','Ygrid','on'); + end + co = get(ax{1},'colororder'); +end + + + +m = length(x_s{1}.x); +for ind = 1:ile-1, + if m == 0, + m = length(x_s{ind+1}.x); + else + m = min([m, length(x_s{ind+1}.x)]); + end +end +k = ceil(p*m); + +%ax1 = newplot; +% ax1=axes('Units','Pixels','Position',[30 30 300 300]); + +% ax2=axes('Units','Pixels','Position',[360 30 300 300]); +for ind = 0:ile-1, + axes(ax{ind+1}) + + % Choose first three colors for head, body, and tail + if no_head == 0, + head{ind+1} = line('color',co(1,:),'linestyle', 'none', 'marker', 'o', 'linewidth', 3, ... + 'xdata',x_s{ind+1}.x(1),'ydata',x_s{ind+1}.y(1)); + end + %body_x = []; body_y = []; + body_x{ind+1} = NaN*ones(1,m); body_y{ind+1} = NaN*ones(1,m); + body_x{ind+1}(1) = x_s{ind+1}.x(1); body_y{ind+1}(1) = x_s{ind+1}.y(1); + body{ind+1} = line('color','b','linestyle','-', 'marker','none', ... %'erase','none', ... + 'xdata',body_x{ind+1},'ydata',body_y{ind+1}); + %tail_x = []; tail_y = []; + tail_x{ind+1} = NaN*ones(1,k+1); tail_y{ind+1} = NaN*ones(1,k+1); + tail_x{ind+1}(1) = x_s{ind+1}.x(1); tail_y{ind+1}(1) = x_s{ind+1}.y(1); + tail{ind+1} = line('color','r','linestyle','-','marker','none', ... %'erase','none', ... + 'xdata',tail_x{ind+1},'ydata',tail_y{ind+1}); + if no_head == 0, + head2{ind+1} = line('color',co(4,:),'linestyle', 'none', 'marker','o', 'linewidth', 3, ... + 'xdata',x_s{ind+1}.x(1),'ydata',x_s{ind+1}.y(1)); + end + + if ile > 2, + h_punkty{ind+1} = line('color','k','linestyle', 'none', 'marker','.', 'markersize', 20, ... %'erase','none', ... + 'xdata',0,'ydata',NaN); + else + h_punkty{ind+1} = line('color','k','linestyle', 'none', 'marker','.', 'markersize', 24, ... %'erase','none', ... + 'xdata',0,'ydata',NaN); + end; + punkty_x{ind+1} = NaN*punkty; punkty_y{ind+1} = NaN*punkty; + punkty_ind{ind+1} = 1; +end; + +% Grow the body +for i = 2:k+1 + for ind = 0:ile-1, + if length(x_s{ind+1}.x) > 0, + if ~isempty(punkty) + if any(punkty == i) + punkty_x{ind+1}(punkty_ind{ind+1}) = x_s{ind+1}.x(i); punkty_y{ind+1}(punkty_ind{ind+1}) = x_s{ind+1}.y(i); + punkty_ind{ind+1} = punkty_ind{ind+1} + 1; + set(h_punkty{ind+1}, 'Xdata', punkty_x{ind+1}, 'Ydata', punkty_y{ind+1}); + end + end + + j = i-1:i; + if no_head == 0, + set(head{ind+1},'xdata',x_s{ind+1}.x(i),'ydata',x_s{ind+1}.y(i)) + set(head2{ind+1},'xdata',x_s{ind+1}.x(i-1),'ydata',x_s{ind+1}.y(i-1)) + end + %set(body{ind+1},'xdata',x_s{ind+1}.x(j),'ydata',x_s{ind+1}.y(j)) + body_x{ind+1}(i) = x_s{ind+1}.x(i); body_y{ind+1}(i) = x_s{ind+1}.y(i); + set(body{ind+1},'xdata',body_x{ind+1},'ydata',body_y{ind+1}) + tail_x{ind+1}(i) = x_s{ind+1}.x(i); tail_y{ind+1}(i) = x_s{ind+1}.y(i); + set(tail{ind+1},'xdata',tail_x{ind+1},'ydata',tail_y{ind+1}) + end + end + + tic; + while toc<time + drawnow + pause(0) + end; + + if (upper(klawisz) == 'Q') | (upper(klawisz) == 27), + set(hf, 'KeyPressFcn', ''); + return; + end + if upper(klawisz) == 13, + set(gcf, 'Color', [1, 1, 0.8]); + klawisz = '~'; +% while length(klawisz) == 0, + while klawisz == '~', + drawnow + pause(0); + end + set(gcf, 'Color', 'w'); + if klawisz ~= 13, + klawisz = '~'; + end + end + if upper(klawisz) == '+', + time = time / 2; + klawisz = '~'; + end + if upper(klawisz) == '-', + time = time * 2; + klawisz = '~'; + end +end + + +% Primary loop +for i = k+2:m + for ind = 0:ile-1, + if length(x_s{ind+1}.x) > 0, + if ~isempty(punkty) + if any(punkty == i) + punkty_x{ind+1}(punkty_ind{ind+1}) = x_s{ind+1}.x(i); punkty_y{ind+1}(punkty_ind{ind+1}) = x_s{ind+1}.y(i); + punkty_ind{ind+1} = punkty_ind{ind+1} + 1; + set(h_punkty{ind+1}, 'Xdata', punkty_x{ind+1}, 'Ydata', punkty_y{ind+1}); + end + end + + j = i-1:i; + if no_head == 0, + set(head{ind+1},'xdata',x_s{ind+1}.x(i),'ydata',x_s{ind+1}.y(i)) + set(head2{ind+1},'xdata',x_s{ind+1}.x(i-1),'ydata',x_s{ind+1}.y(i-1)) + end +% set(body{ind+1},'xdata',x_s{ind+1}.x(j),'ydata',x_s{ind+1}.y(j)) +% set(tail{ind+1},'xdata',x_s{ind+1}.x(j-k),'ydata',x_s{ind+1}.y(j-k)) + tail_x{ind+1}(1:end-1) = tail_x{ind+1}(2:end); tail_y{ind+1}(1:end-1) = tail_y{ind+1}(2:end); + tail_x{ind+1}(end) = x_s{ind+1}.x(i); tail_y{ind+1}(end) = x_s{ind+1}.y(i); + set(tail{ind+1},'xdata',tail_x{ind+1},'ydata',tail_y{ind+1}) + + body_x{ind+1}(i) = x_s{ind+1}.x(i); body_y{ind+1}(i) = x_s{ind+1}.y(i); + set(body{ind+1},'xdata',body_x{ind+1},'ydata',body_y{ind+1}) + end + end + + tic; + while toc<time + drawnow + pause(0); + end + + if (upper(klawisz) == 'Q') | (upper(klawisz) == 27), + set(hf, 'KeyPressFcn', ''); + return; + end; + if upper(klawisz) == 13, + set(gcf, 'Color', [1, 1, 0.8]); + klawisz = '~'; +% while length(klawisz) == 0, + while klawisz == '~', + drawnow + pause(0); + end; + set(gcf, 'Color', 'w'); + if klawisz ~= 13, + klawisz = '~'; + end + end; + if upper(klawisz) == '+', + time = time / 2; + klawisz = '~'; + end; + if upper(klawisz) == '-', + time = time * 2; + klawisz = '~'; + end; +end; + +% Clean up the tail +for i = m+1:m+k + for ind = 0:ile-1, + if length(x_s{ind+1}.x) > 0, + j = i-1:i; + set(tail{ind+1},'xdata',x_s{ind+1}.x(j-k),'ydata',x_s{ind+1}.y(j-k)) + end + end; + + tic; + while toc<time + drawnow + pause(0); %drawnow; + end; + + if (upper(klawisz) == 'Q') | (upper(klawisz) == 27), + set(hf, 'KeyPressFcn', ''); + return; + end; + if upper(klawisz) == 13, + set(gcf, 'Color', [1, 1, 0.8]); + klawisz = '~'; +% while length(klawisz) == 0, + while klawisz == '~', + drawnow + pause(0); + end; + set(gcf, 'Color', 'w'); + if klawisz ~= 13, + klawisz = '~'; + end + end; + if upper(klawisz) == '+', + time = time / 2; + klawisz = '~'; + end; + if upper(klawisz) == '-', + time = time * 2; + klawisz = '~'; + end; + +end; + +set(hf, 'KeyPressFcn', ''); diff --git a/Ex6/rundot.bat b/Ex6/rundot.bat new file mode 100644 index 0000000000000000000000000000000000000000..07d1702910ef26b8aa2c84e6c7fa3b4297f648b3 --- /dev/null +++ b/Ex6/rundot.bat @@ -0,0 +1,10 @@ +path = "d:\Program Files (x86)\Graphviz2.38\bin\";%path% + +del ex6_task1.gif +del ex6_task1b.gif +del ex6_task2.gif +del ex6_task2_new.gif +dot -Tgif ex6_task1.dot -oex6_task1.gif +dot -Tgif ex6_task1b.dot -oex6_task1b.gif +dot -Tgif ex6_task2.dot -oex6_task2.gif +dot -Tgif ex6_task2_new.dot -oex6_task2_new.gif diff --git a/Ex6/rundot_test.bat b/Ex6/rundot_test.bat new file mode 100644 index 0000000000000000000000000000000000000000..8b89638cc7a3fd219c20cbaf3e4247aea4a4140d --- /dev/null +++ b/Ex6/rundot_test.bat @@ -0,0 +1,11 @@ +path = "d:\Program Files (x86)\Graphviz2.38\bin\";%path% + +del ex6_task2_test.gif +dot -Tgif ex6_task2_test.dot -oex6_task2_test.gif +del ex6_task2.gif +dot -Tgif ex6_task2.dot -oex6_task2.gif +del Seria2Parallel-test.gif +dot -Tgif Seria2Parallel-test.dot -oSeria2Parallel-test.gif +del SymbolMapper-test.gif +dot -Tgif SymbolMapper-test.dot -oSymbolMapper-test.gif +